package org.compiere.process;

import java.awt.Frame;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.filechooser.FileNameExtensionFilter;

import jxl.format.Border;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.apps.ADialog;
import org.compiere.apps.AWindow;
import org.compiere.apps.Attachment;
import org.compiere.model.MAttachment;
import org.compiere.model.MAttachmentEntry;
import org.compiere.model.MBCSACRInputFormAppend1;
import org.compiere.model.MBCSACRInputFormAppend2Ext;
import org.compiere.model.MBCSACRInputFormAppend3_1;
import org.compiere.model.MBCSACRInputFormAppend3_1Ext;
import org.compiere.model.MBCSACRInputFormAppend3_2Ext;
import org.compiere.model.MBCSACRInputFormAppend4;
import org.compiere.model.MBCSACRInputFormCompany;
import org.compiere.model.MBCSACRInputFormPeriod;
import org.compiere.model.MBCSFRInputForm;
import org.compiere.model.MBCSFRInputFormColumn;
import org.compiere.model.MBCSFRInputFormCompany;
import org.compiere.model.MBCSFRInputFormLine;
import org.compiere.model.MBCSFRInputFormPeriod;
import org.compiere.model.MBCSFRSetupInputForm;
import org.compiere.model.MBPartner;
import org.compiere.model.MOrg;
import org.compiere.model.MPeriod;
import org.compiere.model.MWindow;
import org.compiere.swing.CPanel;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.joda.time.DateTime;

import extend.org.compiere.hrm.ExcelCell;
import extend.org.compiere.utils.Util;

import org.utils.*;

public class BCS_FRInputCompanyUpload extends SvrProcess {

	/** Current context */
	private Properties m_ctx;
	/**     */
	private ProcessInfo pi;

	private String Period = null;
	private BigDecimal Year = null;
	private int iYear = 0;
	private boolean isConsolidation = false;
	private BigDecimal C_BPartner = null;
	private String Application = "";

	/** */
	private String path = "";

	private int AD_Org_ID = -1;
	private int AD_Client_ID = -1;
	BigDecimal zero = Env.ZERO;
	MBPartner bp = null;
	
	
	private static CLogger s_log = CLogger.getCLogger(BCS_FRInputCompanyUpload.class);
	HSSFWorkbook wb;
	/**
	 * Prepare - e.g., get Parameters.
	 */
	@Override
	protected void prepare() {
		log.info("");
		m_ctx = Env.getCtx();
		pi = getProcessInfo();
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++) {
			String name = para[i].getParameterName();
			if (para[i].getParameter() == null)
				;
			else
				log.log(Level.SEVERE, "Unknown Parameter: " + name);
		}

		path = Util.localFilePath;
	}

	@Override
	protected String doIt() throws Exception {
		String result = "";
		
		MAttachmentEntry entry = null;
		MAttachment attachment  = null;
		
		zero = zero.setScale(0, BigDecimal.ROUND_HALF_UP);
	
//		MWindow w = new MWindow(Env.getCtx(), 1000121, null);
//		
		
		
//		CPanel cp = new CPanel(new AWindow());
//		Env.getFrame(w.);
//		Frame frame = new Frame();
//		
//		System.out.println(frame);
//
//		Attachment v = new Attachment (Env.getFrame(frame), 2, 0, 1000273, getRecord_ID(), null);		
//		System.out.println(v);
		
		
		//--------------------------
		System.out.println("Start: " + new Timestamp(System.currentTimeMillis()));
		
		try {
			
			File file = null;
			boolean isMother = false;
			int MBCSFRInputFormCompany_ID = getRecord_ID();
			if(MBCSFRInputFormCompany_ID == -1)
				return result;
			
			MBCSFRInputFormCompany company = new MBCSFRInputFormCompany(Env.getCtx(),MBCSFRInputFormCompany_ID, null);
			bp = (MBPartner) company.getC_BPartner();
			if(bp.getC_BPartner_ID() == 1000084)
//			int ad_org = DB.getSQLValue(null, "Select ad_orgtype_id from adempiere.ad_orginfo where ad_org_id = " + bp.getAD_OrgBP_ID_Int());
//			if(ad_org == 1000000)
				isMother = true;		
//			if(!isMother){
				// try{
//				 final JFileChooser fc = new JFileChooser();
//				 JList list2 = new JList();
//		//		 fc.remove(0);
//				 fc.addChoosableFileFilter(new FileNameExtensionFilter("xls", "xls"));
//				  fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
//				// FileOutputStream fos = null;
//				// OutputStreamWriter out = null;
//				//
//				 String FileName = "";
//				 if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list2)) 
//					 FileName = fc.getSelectedFile().getAbsolutePath();
//				 
//				 if(FileName.equals(""))
//					 return "";
//				// "/Swift.txt";
//		//		String FileName = "/home/alm/Documents/work/_Consolidation/K_KODMMGGGG2.xls";
//				file = new File(FileName);// fc.getSelectedFile();
//				Attachment v = new Attachment (Env.getFrame(null), 2,	0, 1000273, getRecord_ID(), null);
//				v.loadAttachmentsExt();
//				String fileName = v.getFileNameExt(0);
				
				attachment = new MAttachment(Env.getCtx(), 1000273, getRecord_ID(), null);//.getAttachment();
				System.out.println(attachment);
				
				if(attachment == null)
					throw new AdempiereException(Msg.translate(m_ctx, "AttachmentNotFound"));
				
//				MAttachmentEntry entry = null;
				MAttachmentEntry[] list555 = attachment.getEntries();
				if(list555.length == 1)
					entry = list555[0];			
				if(entry == null && !isMother)
					return result;
//			}

			//total----------------------------------------------
			List<Line> list;

			
				
			if(isMother && entry == null){
			// create all forms{
				createAllForms(company);
				
				System.out.println("Created forms: " + new Timestamp(System.currentTimeMillis()));
				
				List<MBCSFRInputForm> forms = getMBCSFRInputForm(company);
				
				for(MBCSFRInputForm form: forms){
					int row = 0;
					switch (form.getCode()){
						case 1:
							list = load1(company);
							break;
						case 2:
							list = loadICS1(company, form);
							break;
						case 3:
							list = load2(company);
							break;
						case 4:
							list = loadICS2(company, form);
							break;
						case 5:
							list = load5(company);
							break;
						case 6:
							list = loadICS3(company, form);
							break;							
						case 7:
							list = load7(company);
							break;
						case 8:
							list = load8(company);
							createPosition8(form, list, 0);
							break;
						case 9:
							list = load9(company);
							break;
						case 10:
							list = load10(company);
							break;
						case 12:								//8
							list = load12(company);
							createPosition12(form, list, 0);
							break;							
						case 11:
							list = load11(company);
							break;
						case 13:
							list = load13(company);
							break;
						case 14:
							list = load14(company);
							break;					
						case 15:
							list = load15(company);
							break;					
						case 16:
							list = load16(company);
							break;
						case 17:
							list = load17(company);
							break;	
						case 18:
							list = load18(company);
							break;					
						case 19:
							list = load19(company);
							createPosition19(form, list, 0);
							break;
						case 20:
							list = load20(company);
							break;
						case 21:
							list = load21(company);
							break;
						case 22:
							list = load22(company);
							break;					
						case 23:
							list = load23(company);
							break;							
						default:
							continue;
					}
					if(list != null && list.size() > 0){// eq----------
						
						List<MBCSFRInputForm> lform = new ArrayList<MBCSFRInputForm>();
						lform.add(form);
						
						List<MBCSFRInputFormLine> lines = getAllForms_MBCSFRInputFormLines(lform);
						List<MBCSFRInputFormColumn> columns = getMBCSFRInputFormColumn(lines);
						
						for(MBCSFRInputFormLine line: lines){
							if(line.getBCS_FRInputForm_ID() == form.get_ID() && list.size() > row){
								int c = 0;
								for(MBCSFRInputFormColumn column: columns){
									if(column.getBCS_FRInputFormLine_ID() == line.get_ID()){
										if(c == 0)
											column.setValue1(setZero2Space(list.get(row).getValue1()));
										else if(c == 1)
											column.setValue1(setZero2Space(list.get(row).getValue2()));
										else if(c == 2)
											column.setValue1(setZero2Space(list.get(row).getValue3()));
										else if(c == 3)
											column.setValue1(setZero2Space(list.get(row).getValue4()));
										else if(c == 4)
											column.setValue1(setZero2Space(list.get(row).getValue5()));
										else if(c == 5)
											column.setValue1(setZero2Space(list.get(row).getValue6()));
										else if(c == 6)
											column.setValue1(setZero2Space(list.get(row).getValue7()));
										else if(c == 7)
											column.setValue1(setZero2Space(list.get(row).getValue8()));											
										else if(c == 8)
											column.setValue1(setZero2Space(list.get(row).getValue9()));											
										else if(c == 9)
											column.setValue1(setZero2Space(list.get(row).getValue10()));											
										else if(c == 10)
											column.setValue1(setZero2Space(list.get(row).getValue11()));											
										else if(c == 11)
											column.setValue1(list.get(row).getValue12());											
										else if(c == 12)
											column.setValue1(list.get(row).getValue13());											
										
										column.save();
										c++;
									}
								}//columns
								row++;
							}
						}//lines
						
					}
					
					
				}
				
				
			//}
				
				
				
			//load from file	
			}else{	
			
				createAllForms(company);
				
				List<MBCSFRInputForm> forms = getMBCSFRInputForm(company); 
//			wb = new HSSFWorkbook(new FileInputStream(file));
		InputStream inputStream = entry.getInputStream();  
		wb = new HSSFWorkbook(inputStream);
			
			
			List<String> namesOfSheet = new ArrayList<String>();
			int count = wb.getNumberOfSheets();
			for(int i=0; i < count; i++)
				namesOfSheet.add(wb.getSheetName(i));

			for(int i=0; i < count; i++){
				int row = 0;
				String s = namesOfSheet.get(i);
				int diff = s.indexOf(" ");
				if(diff == -1)
					continue;
				s = s.substring(0, diff);
				
				int code = -1;
				
				try{
					code = Integer.parseInt(s);
				}catch(NumberFormatException e){
//					s_log.log(Level.SEVERE, e.getMessage());
					continue;
				}
				
				boolean is = false;
				for(int j = 0; j < forms.size(); j++){
					if(forms.get(j).getCode() == code){
						is = true;
						break;
					}
				}
				
				if(!is){
				// get datas from system
//					list = load1();
//				}else{
				// get datas from file .xls	
		//					create form (lines, columns)
					
											MBCSFRInputForm newForm = null;
							
											PreparedStatement pstmt = null;
											ResultSet rs = null;
											String sql = "";
						
											// check existing lines
											sql = " SELECT BCS_FRSetupInputForm_ID FROM BCS_FRSetupInputForm \n "
													+ " WHERE code =" + code;
											int SetupFormID = 0;
											try {
												pstmt = DB.prepareStatement(sql, null);
												rs = pstmt.executeQuery();
												if (rs.next()) {
													SetupFormID = rs.getInt(1);
													newForm = new MBCSFRInputForm(Env.getCtx(), 0, null);
													newForm.setBCS_FRSetupInputForm_ID(SetupFormID);
													newForm.setBCS_FRInputForm_Company_ID(MBCSFRInputFormCompany_ID);
													newForm.save(null);
													//forms.add(newForm);
												}
											} catch (SQLException e) {
												s_log.log(Level.SEVERE, "BCS_FRInputForm", e);
											} finally {
												DB.close(rs, pstmt);
												rs = null;
												pstmt = null;
											}
						
						}
						
				}
					
					
					forms = getMBCSFRInputForm(company); 
					//gooooooooooo
					for(MBCSFRInputForm form: forms){
		
						if(
								form.getCode() != 1						
							&&	form.getCode() != 2						
							&&	form.getCode() != 3
							&&	form.getCode() != 4
							&&	form.getCode() != 5						
							&&	form.getCode() != 6
							&&	form.getCode() != 7
							&&	form.getCode() != 8							
							&&	form.getCode() != 9						
							&&  form.getCode() != 10 
							&&	form.getCode() != 11
							&& 	form.getCode() != 12
							&&	form.getCode() != 13					
							&&	form.getCode() != 14
							&&	form.getCode() != 15 
							&&	form.getCode() != 16
							&&	form.getCode() != 17
							&&	form.getCode() != 18	
							&&	form.getCode() != 19
							&&	form.getCode() != 20						
							&&	form.getCode() != 21 
							&&	form.getCode() != 22
							&&	form.getCode() != 23						
							)
		//				else
							continue;
						//}
						
						for(int i=0; i < count; i++){
							int row = 0;
							String s = namesOfSheet.get(i);
							int diff = s.indexOf(" ");
							if(diff == -1)
								continue;
							s = s.substring(0, diff);
							
							int code = -1;
							
							try{
								code = Integer.parseInt(s);
							}catch(NumberFormatException e){
		//						s_log.log(Level.SEVERE, e.getMessage());
								continue;
							}
							
							if(code != -1 && form.getCode() == code){
								
								list = new ArrayList<Line>();
								//read need sheet (special each)
								switch(form.getCode()){
									case 1:
										list = read1(wb.getSheetAt(i), list);
										break;
									case 2:
										list = read2(wb.getSheetAt(i), list);
										break;
									case 3:
										list = read3(wb.getSheetAt(i), list);
										break;
									case 4:
										list = read4(wb.getSheetAt(i), list);
										break;								
									case 5:
										list = read5(wb.getSheetAt(i), list);
										break;								
									case 6:
										list = read6(wb.getSheetAt(i), list);
										break;
									case 7:
										list = read7(wb.getSheetAt(i), list);
										break;
									case 8:
										list = read8(wb.getSheetAt(i), list);
										createPosition8(form, list, code);
										break;
									case 9:
										list = read9(wb.getSheetAt(i), list);
										break;
									case 10:
										list = read10(wb.getSheetAt(i), list);
										break;
									case 11:
										list = read11(wb.getSheetAt(i), list);
										break;
									case 12:
										list = read12(wb.getSheetAt(i), list);
										createPosition12(form, list, code);
										break;
									case 13:
										list = read13(wb.getSheetAt(i), list);
										break;
									case 14:
										list = read14(wb.getSheetAt(i), list);
										break;
									case 15:
										list = read15(wb.getSheetAt(i), list);
										break;
									case 16:
										list = read16(wb.getSheetAt(i), list);
										break;
									case 17:
										list = read17(wb.getSheetAt(i), list);
										break;
									case 18:
										list = read18(wb.getSheetAt(i), list);
										break;																
									case 19:
										list = read19(wb.getSheetAt(i), list);
										createPosition19(form, list, code);
										break;															
									case 20:
										list = read20(wb.getSheetAt(i), list);
										break;																
									case 21:
										list = read21(wb.getSheetAt(i), list);
										break;
									case 22:
										list = read22(wb.getSheetAt(i), list);
										break;
									case 23:
										list = read23(wb.getSheetAt(i), list);
										break;								
								}
								
								
								if(list != null && list.size() > 0){// eq----------
									
									List<MBCSFRInputForm> lform = new ArrayList<MBCSFRInputForm>();
									lform.add(form);
									
									List<MBCSFRInputFormLine> lines = getAllForms_MBCSFRInputFormLines(lform);
									List<MBCSFRInputFormColumn> columns = getMBCSFRInputFormColumn(lines);
									
									for(MBCSFRInputFormLine line: lines){
										if(line.getBCS_FRInputForm_ID() == form.get_ID() && list.size() > row){
											int c = 0;
											for(MBCSFRInputFormColumn column: columns){
												if(column.getBCS_FRInputFormLine_ID() == line.get_ID()){
													if(c == 0)
														column.setValue1(setZero2Space(list.get(row).getValue1()));
													else if(c == 1)
														column.setValue1(setZero2Space(list.get(row).getValue2()));
													else if(c == 2)
														column.setValue1(setZero2Space(list.get(row).getValue3()));
													else if(c == 3)
														column.setValue1(setZero2Space(list.get(row).getValue4()));
													else if(c == 4)
														column.setValue1(setZero2Space(list.get(row).getValue5()));
													else if(c == 5)
														column.setValue1(setZero2Space(list.get(row).getValue6()));
													else if(c == 6)
														column.setValue1(setZero2Space(list.get(row).getValue7()));
													else if(c == 7)
														column.setValue1(setZero2Space(list.get(row).getValue8()));											
													else if(c == 8)
														column.setValue1(setZero2Space(list.get(row).getValue9()));											
													else if(c == 9)
														column.setValue1(setZero2Space(list.get(row).getValue10()));											
													else if(c == 10)
														column.setValue1(setZero2Space(list.get(row).getValue11()));											
													else if(c == 11)
														column.setValue1(list.get(row).getValue12());											
													else if(c == 12)
														column.setValue1(list.get(row).getValue13());											
													
													column.save();
													c++;
												}
											}//columns
											row++;
										}
									}//lines
									
								}
								
							}
						}					
					}
			}
			
			
		} catch (Exception e) {
			return e.toString();
		}finally{
			if(attachment != null)
				attachment.delete(true);
		}

		System.out.println("End: " + new Timestamp(System.currentTimeMillis()));
		
		return result;
	}

	private void createAllForms(MBCSFRInputFormCompany company) {
		
//		List<MBCSFRInputFormCompany> icompany = new ArrayList<MBCSFRInputFormCompany>();
//		icompany.add(company);
		List<MBCSFRInputForm> forms = getMBCSFRInputForm(company);
		List<MBCSFRSetupInputForm> sforms = getMBCSFRSetupInputForm();
		
		
		for(MBCSFRSetupInputForm sform: sforms){
			boolean is = false;
			for(MBCSFRInputForm form: forms){
				if(sform.getBCS_FRSetupInputForm_ID() == form.getBCS_FRSetupInputForm_ID())
					is = true;
			}
			if(!is){
				MBCSFRInputForm newForm = new MBCSFRInputForm(Env.getCtx(), 0, null);
				newForm.setBCS_FRSetupInputForm_ID(sform.getBCS_FRSetupInputForm_ID());
				newForm.setBCS_FRInputForm_Company_ID(company.get_ID());
				newForm.save();
			}
		}
		
	}
	
	private List<MBCSFRSetupInputForm> getMBCSFRSetupInputForm(){
		
		List<MBCSFRSetupInputForm> forms = new ArrayList<MBCSFRSetupInputForm>();
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";

		sql = " SELECT BCS_FRSetupInputForm_ID FROM BCS_FRSetupInputForm order by BCS_FRSetupInputForm_ID\n ";
		int SetupFormID = 0;
		try {
			pstmt = DB.prepareStatement(sql, null);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				SetupFormID = rs.getInt(1);
				MBCSFRSetupInputForm newForm = new MBCSFRSetupInputForm(Env.getCtx(), SetupFormID, null);
				forms.add(newForm);
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "BCS_FRInputForm", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
		
		return forms;
	}

	private void createPosition12(MBCSFRInputForm form,  List<Line> list, int code){
		if(list != null && list.size() > 0){
			List<MBCSFRInputForm> lform = new ArrayList<MBCSFRInputForm>();
			lform.add(form);
			List<MBCSFRInputFormLine> lines = getAllForms_MBCSFRInputFormLines(lform);
			List<MBCSFRInputFormLine> oneline = new ArrayList<MBCSFRInputFormLine>();
			oneline.add(lines.get(0));
			List<MBCSFRInputFormColumn> columns = getMBCSFRInputFormColumn(oneline);
			if(lines.size() > 0){
				int diff = list.size() - lines.size();
				if(diff > 0){
					for(int i=0; i< diff; i++){
						MBCSFRInputFormLine line = new MBCSFRInputFormLine(Env.getCtx(), 0, null);
						line.setBCS_FRInputForm_ID(form.get_ID());
						line.setBCS_FRPositionsDict_ID(lines.get(0).getBCS_FRPositionsDict_ID());
						line.save();
						for(MBCSFRInputFormColumn column: columns){
							String sql2 = 
									"INSERT INTO adempiere.bcs_frinputformcolumn(	\n"	+
											"ad_client_id, " +
											"ad_org_id, " +
											"bcs_frinputformcolumn_id, " +
											"created, " +
											"createdby,	\n" + 
											"isactive, " +
											"updated, " +
											"updatedby, " +
											"value1, " +
											"bcs_frinputformline_id,	\n"	+ 
											"bcs_frcolumnsdict_id, " +
											"seqno" +
										")	\n"	+
										"VALUES (	\n"	+
										Env.getAD_Client_ID(Env.getCtx()) + ",	\n"	+ 
										Env.getAD_Org_ID(Env.getCtx()) + ",	\n"	+  
										"(select max(bcs_frinputformcolumn_id)+1 from adempiere.bcs_frinputformcolumn),	\n"	+ 
										"'" + new Timestamp(System.currentTimeMillis()) +  "',	\n"	+ 
										Env.getAD_User_ID(Env.getCtx())+",	\n"	+ 
										"'Y',	\n"	+ 
										"'" + new Timestamp(System.currentTimeMillis()) +  "',	\n"	+ 
										Env.getAD_User_ID(Env.getCtx())+",	\n"	+ 
										"'',	\n"	+ 
										line.get_ID() + ",	\n"	+ 
										column.getBCS_FRColumnsDict_ID() + ", 	\n"	+
										column.getSeqNo() + ")	\n"	;
										
							DB.executeUpdate(sql2);
							
							
//							MBCSFRInputFormColumn col = new MBCSFRInputFormColumn(Env.getCtx(), 0, null);
//							col.setBCS_FRInputFormLine_ID(line.get_ID());
//							col.setBCS_FRColumnsDict_ID(column.getBCS_FRColumnsDict_ID());
//							col.save();
							
						}
					}
					//changes PosDictID 2 and last record
					// 2 = footer and summary
					lines = getAllForms_MBCSFRInputFormLines(lform);
					int footer = lines.get(1).getBCS_FRPositionsDict_ID();//
					int last = lines.get(lines.size()-1).getBCS_FRPositionsDict_ID();
					
					MBCSFRInputFormLine l = lines.get(1);
					l.setBCS_FRPositionsDict_ID(last);
					l.save(null);
					
					l = lines.get(lines.size()-1);
					l.setBCS_FRPositionsDict_ID(footer);
					l.save(null);
					
					
					
					
				}
			}
		}
	}

	private void createPosition8(MBCSFRInputForm form,  List<Line> list, int code){
		if(list != null && list.size() > 0){
			List<MBCSFRInputForm> lform = new ArrayList<MBCSFRInputForm>();
			lform.add(form);
			List<MBCSFRInputFormLine> lines = getAllForms_MBCSFRInputFormLines(lform);
			List<MBCSFRInputFormLine> oneline = new ArrayList<MBCSFRInputFormLine>();
			oneline.add(lines.get(0));
			List<MBCSFRInputFormColumn> columns = getMBCSFRInputFormColumn(oneline);
			if(lines.size() > 0){
				int diff = list.size() - lines.size();
				if(diff > 0){
					for(int i=0; i< diff; i++){
						MBCSFRInputFormLine line = new MBCSFRInputFormLine(Env.getCtx(), 0, null);
						line.setBCS_FRInputForm_ID(form.get_ID());
						line.setBCS_FRPositionsDict_ID(lines.get(0).getBCS_FRPositionsDict_ID());
						line.save();
						for(MBCSFRInputFormColumn column: columns){
							String sql2 = 
									"INSERT INTO adempiere.bcs_frinputformcolumn(	\n"	+
											"ad_client_id, " +
											"ad_org_id, " +
											"bcs_frinputformcolumn_id, " +
											"created, " +
											"createdby,	\n" + 
											"isactive, " +
											"updated, " +
											"updatedby, " +
											"value1, " +
											"bcs_frinputformline_id,	\n"	+ 
											"bcs_frcolumnsdict_id, " +
											"seqno" +
										")	\n"	+
										"VALUES (	\n"	+
										Env.getAD_Client_ID(Env.getCtx()) + ",	\n"	+ 
										Env.getAD_Org_ID(Env.getCtx()) + ",	\n"	+  
										"(select max(bcs_frinputformcolumn_id)+1 from adempiere.bcs_frinputformcolumn),	\n"	+ 
										"'" + new Timestamp(System.currentTimeMillis()) +  "',	\n"	+ 
										Env.getAD_User_ID(Env.getCtx())+",	\n"	+ 
										"'Y',	\n"	+ 
										"'" + new Timestamp(System.currentTimeMillis()) +  "',	\n"	+ 
										Env.getAD_User_ID(Env.getCtx())+",	\n"	+ 
										"'',	\n"	+ 
										line.get_ID() + ",	\n"	+ 
										column.getBCS_FRColumnsDict_ID() + ", 	\n"	+
										column.getSeqNo() + ")	\n"	;
										
							DB.executeUpdate(sql2);
							
							
//							MBCSFRInputFormColumn col = new MBCSFRInputFormColumn(Env.getCtx(), 0, null);
//							col.setBCS_FRInputFormLine_ID(line.get_ID());
//							col.setBCS_FRColumnsDict_ID(column.getBCS_FRColumnsDict_ID());
//							col.save();
							
						}
					}
					//changes PosDictID 2 and last record
					// 2 = footer and summary
					lines = getAllForms_MBCSFRInputFormLines(lform);
					int footer = lines.get(1).getBCS_FRPositionsDict_ID();//
					int last = lines.get(lines.size()-1).getBCS_FRPositionsDict_ID();
					
					MBCSFRInputFormLine l = lines.get(1);
					l.setBCS_FRPositionsDict_ID(last);
					l.save(null);
					
					l = lines.get(lines.size()-1);
					l.setBCS_FRPositionsDict_ID(footer);
					l.save(null);
					
					
					
					
				}
			}
		}
	}
	
	
	private void createPosition19(MBCSFRInputForm form,  List<Line> list, int code){
		if(list != null && list.size() > 0){
			List<MBCSFRInputForm> lform = new ArrayList<MBCSFRInputForm>();
			lform.add(form);
			List<MBCSFRInputFormLine> lines = getAllForms_MBCSFRInputFormLines(lform);
			List<MBCSFRInputFormLine> oneline = new ArrayList<MBCSFRInputFormLine>();
			oneline.add(lines.get(0));
			List<MBCSFRInputFormColumn> columns = getMBCSFRInputFormColumn(oneline);
			if(lines.size() > 0){
				int diff = list.size() - lines.size();
				if(diff > 0){
					for(int i=0; i< diff; i++){
						MBCSFRInputFormLine line = new MBCSFRInputFormLine(Env.getCtx(), 0, null);
						line.setBCS_FRInputForm_ID(form.get_ID());
						line.setBCS_FRPositionsDict_ID(lines.get(1).getBCS_FRPositionsDict_ID());
						line.save();
						for(MBCSFRInputFormColumn column: columns){
							
							String sql2 = 
									"INSERT INTO adempiere.bcs_frinputformcolumn(	\n"	+
											"ad_client_id, " +
											"ad_org_id, " +
											"bcs_frinputformcolumn_id, " +
											"created, " +
											"createdby,	\n" + 
											"isactive, " +
											"updated, " +
											"updatedby, " +
											"value1, " +
											"bcs_frinputformline_id,	\n"	+ 
											"bcs_frcolumnsdict_id, " +
											"seqno" +
										")	\n"	+
										"VALUES (	\n"	+
										Env.getAD_Client_ID(Env.getCtx()) + ",	\n"	+ 
										Env.getAD_Org_ID(Env.getCtx()) + ",	\n"	+  
										"(select max(bcs_frinputformcolumn_id)+1 from adempiere.bcs_frinputformcolumn),	\n"	+ 
										"'" + new Timestamp(System.currentTimeMillis()) +  "',	\n"	+ 
										Env.getAD_User_ID(Env.getCtx())+",	\n"	+ 
										"'Y',	\n"	+ 
										"'" + new Timestamp(System.currentTimeMillis()) +  "',	\n"	+ 
										Env.getAD_User_ID(Env.getCtx())+",	\n"	+ 
										"'',	\n"	+ 
										line.get_ID() + ",	\n"	+ 
										column.getBCS_FRColumnsDict_ID() + ", 	\n"	+
										column.getSeqNo() + ")	\n"	;
										
							DB.executeUpdate(sql2);
							
//							MBCSFRInputFormColumn col = new MBCSFRInputFormColumn(Env.getCtx(), 0, null);
//							col.setBCS_FRInputFormLine_ID(line.get_ID());
//							col.setBCS_FRColumnsDict_ID(column.getBCS_FRColumnsDict_ID());
//							col.save();
							
						}
					}
				}
			}
		}
	}
	
	
	private String setZero2Space(String v){
		if(v == null || v.equals("0.0"))
				v = "";
		return v;
	}
	

	private List<Line> load1(MBCSFRInputFormCompany company) {
 		List<Line> list = new ArrayList<Line>();
		
		MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
		int C_Period_ID = period.getPeriodTo();
		MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);

		Timestamp ts = p.getEndDate();
		
		Timestamp tsStartDate = p.getStartDate();
		
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(ts.getTime());
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.MONTH, 0);
		tsStartDate.setTime(c.getTimeInMillis());
		
		
		Timestamp tsEndDate = p.getEndDate();
		
		// ts2

		Date d = new Date(ts.getTime());// ts.getDate();
		c = Calendar.getInstance();
		c.setTime(d);
//		c.add(Calendar.DAY_OF_MONTH, 1);
		d.setTime(c.getTimeInMillis());
		ts.setTime(d.getTime());
		Date date = null;
		int year = c.get(Calendar.YEAR);
	    
		String pattern = "MM/dd/yyyy";
	    SimpleDateFormat format = new SimpleDateFormat(pattern);
	    try {
	      date = format.parse("01/01/" + year);
//	      System.out.println(date);
	    } catch (ParseException e) {
	      e.printStackTrace();
	      date = new Date(ts.getTime());
	    }		


		c.setTime(date);
	    
		c.add(Calendar.DAY_OF_MONTH, -1);
		date = c.getTime();
		
		Timestamp tsBeginOfYear = new Timestamp(date.getTime());
		
//		List<Integer> codes = new ArrayList<Integer>();
		
		BigDecimal r100 = zero;	BigDecimal r100p = zero;
		BigDecimal r200 = zero;	BigDecimal r200p = zero;
		BigDecimal r300 = zero;	BigDecimal r300p = zero;
		BigDecimal r400 = zero;	BigDecimal r400p = zero;
		BigDecimal r420 = zero;	BigDecimal r420p = zero;
		BigDecimal r500 = zero;	BigDecimal r500p = zero;	
		
		BigDecimal r414 = zero;	BigDecimal r414p = zero;
		BigDecimal r413 = zero;	BigDecimal r413p = zero;
		BigDecimal r412 = zero;	BigDecimal r412p = zero;
		BigDecimal r411 = zero;	BigDecimal r411p = zero;
		BigDecimal r410 = zero;	BigDecimal r410p = zero;
		BigDecimal r316 = zero;	BigDecimal r316p = zero;
		BigDecimal r315 = zero;	BigDecimal r315p = zero;
		BigDecimal r314 = zero;	BigDecimal r314p = zero;
		BigDecimal r313 = zero;	BigDecimal r313p = zero;
		BigDecimal r312 = zero;	BigDecimal r312p = zero;
		BigDecimal r310 = zero;	BigDecimal r310p = zero;
		BigDecimal r217 = zero;	BigDecimal r217p = zero;
		BigDecimal r216 = zero;	BigDecimal r216p = zero;
		BigDecimal r215 = zero;	BigDecimal r215p = zero;
		BigDecimal r214 = zero;	BigDecimal r214p = zero;
		BigDecimal r213 = zero;	BigDecimal r213p = zero;
		BigDecimal r212 = zero;	BigDecimal r212p = zero;
		BigDecimal r210 = zero;	BigDecimal r210p = zero;
		BigDecimal r123 = zero;	BigDecimal r123p = zero;
		BigDecimal r122 = zero;	BigDecimal r122p = zero;
		BigDecimal r121 = zero;	BigDecimal r121p = zero;
		BigDecimal r120 = zero;	BigDecimal r120p = zero;
		BigDecimal r119 = zero;	BigDecimal r119p = zero;
		BigDecimal r118 = zero;	BigDecimal r118p = zero;
		BigDecimal r117 = zero;	BigDecimal r117p = zero;
		BigDecimal r116 = zero;	BigDecimal r116p = zero;
		BigDecimal r115 = zero;	BigDecimal r115p = zero;
		BigDecimal r114 = zero;	BigDecimal r114p = zero;
		BigDecimal r113 = zero;	BigDecimal r113p = zero;
		BigDecimal r111 = zero;	BigDecimal r111p = zero;
		BigDecimal r019 = zero;	BigDecimal r019p = zero;
		BigDecimal r110 = zero;	BigDecimal r110p = zero;
		BigDecimal r101 = zero;	BigDecimal r101p = zero;
		BigDecimal r018 = zero;	BigDecimal r018p = zero;
		BigDecimal r017 = zero;	BigDecimal r017p = zero;
		BigDecimal r016 = zero;	BigDecimal r016p = zero;
		BigDecimal r015 = zero;	BigDecimal r015p = zero;
		BigDecimal r014 = zero;	BigDecimal r014p = zero;
		BigDecimal r013 = zero;	BigDecimal r013p = zero;
		BigDecimal r011 = zero;	BigDecimal r011p = zero;
		BigDecimal r010 = zero;	BigDecimal r010p = zero;

		//get datas-----------------------------------------------------------------
		String codes = "1000,1030,1040,1050";//.add(1000); codes.add(1030); codes.add(1040); codes.add(1050);
		r010 = getData(codes, ts, "dr", "");	r010p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="114000";//.clear(); codes.add(114000);
		r011 = getData(codes, ts, "dr", "");	r011p = getData(codes, tsBeginOfYear, "dr", "");		

		codes = "";//.clear(); codes.add(113000);
		r013 = getData(codes, ts, "dr", "");	r013p = getData(codes, tsBeginOfYear, "dr", "");	

		codes = "";//.clear(); codes.add(113000);
		r014 = getData(codes, ts, "dr", ""); r014p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="111000,112000,115000,1060,1070,127000,127000_15";//.clear(); codes.add(111000);codes.add(112000);codes.add(115000);codes.add(1060);codes.add(1070);//codes.add(127000);codes.add(127000_15);
		r015 = getData(codes, ts, "dr", "");	r015p = getData(codes, tsBeginOfYear, "dr", "");	

 		codes="1200,1250,1280,127000,127000_16";//.clear(); codes.add(1200);codes.add(1250);codes.add(1280);//codes.add(127000);codes.add(127000_16);
		r016 = getData(codes, ts, "dr", "");	r016p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="141000";//.clear(); codes.add(141000);
		r017 = getData(codes, ts, "dr", "");	r017p = getData(codes, tsBeginOfYear, "dr", "");

		codes="1300,1340,1350,1360";//.clear(); codes.add(1300);codes.add(1340);codes.add(1350);codes.add(1360);
		r018 = getData(codes, ts, "dr", "");	r018p = getData(codes, tsBeginOfYear, "dr", "");

		codes="142000,143000,1600";//.clear(); codes.add(142000);codes.add(143000);codes.add(1600);
		r019 = getData(codes, ts, "dr", "");	r019p = getData(codes, tsBeginOfYear, "dr", "");

		codes="1500";//.clear(); codes.add(1500);
		r101 = getData(codes, ts, "dr", "");	r101p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="203000";//.clear(); codes.add(203000);
		r110 = getData(codes, ts, "dr", "");	r110p = getData(codes, tsBeginOfYear, "dr", "");

		codes="";//.clear(); codes.add(111000);codes.add(112000);codes.add(115000);codes.add(1060);codes.add(1070);//codes.add(127000);//codes.add(127000_15);
		r111 = getData(codes, ts, "dr", "");		r111p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="202000";//.clear(); codes.add(202000);
		r113 = getData(codes, ts, "dr", "");	r113p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="201000,204000";//.clear(); codes.add(201000);codes.add(204000);
		r114 = getData(codes, ts, "dr", "");	r114p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="2100,2150,2180";//.clear(); codes.add(2100);codes.add(2150);codes.add(2180);
		r115 = getData(codes, ts, "dr", "");	r115p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="2200";//.clear(); codes.add(2200);
		r116 = getData(codes, ts, "dr", "");	r116p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="2300";//.clear(); codes.add(2300);
		r117 = getData(codes, ts, "dr", "");	r117p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="2400";//.clear(); codes.add(2400);
		r118 = getData(codes, ts, "dr", "");	r118p = getData(codes, tsBeginOfYear, "dr", "");		

		codes="2500";//.clear(); codes.add(2500);
		r119 = getData(codes, ts, "dr", "");	r119p = getData(codes, tsBeginOfYear, "dr", "");

		codes="2600";//.clear(); codes.add(2600);
		r120 = getData(codes, ts, "dr", "");	r120p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="2700";//.clear(); codes.add(2700);
		r121 = getData(codes, ts, "dr", "");	r121p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="2800";//.clear(); codes.add(2800);
		r122 = getData(codes, ts, "dr", "");	r122p = getData(codes, tsBeginOfYear, "dr", "");	

		codes="2900,2930";//.clear(); codes.add(2900);codes.add(2930);
		r123 = getData(codes, ts, "dr", "");	r123p = getData(codes, tsBeginOfYear, "dr", "");	

		
		
		r200 = r200.add(r110).add(r111).add(r113).add(r114).add(r115).add(r116).add(r117).add(r118).add(r119).add(r120).add(r121).add(r122).add(r123);
		r200p = r200p.add(r110p).add(r111p).add(r113p).add(r114p).add(r115p).add(r116p).add(r117p).add(r118p).add(r119p).add(r120p).add(r121p).add(r122p).add(r123p);
//		codes = "203000,202000,201000,204000,2100,2150,2180,2200,2300,2400,2500,2600,2700,2800,2900,2930";
//		r200 = getData(codes, ts, "dr", "");	r200p = getData(codes, tsBeginOfYear, "dr", "");
		
		codes="301000,302000";//.clear(); codes.add(301000);codes.add(302000);
		r210 = getData(codes, ts, "cr", "");	r210p = getData(codes, tsBeginOfYear, "cr", "");	

		codes="303000,304000,305000";//.clear(); codes.add(303000);codes.add(304000);codes.add(305000);
		r212 = getData(codes, ts, "cr", "");	r212p = getData(codes, tsBeginOfYear, "cr", "");	

			 //331000,332000,333000,334000,336000,337000,338000,3390		
		codes="331000,332000,333000,334000,336000,337000,338000,3390";//.clear(); codes.add(331000);codes.add(332000);codes.add(333000);codes.add(334000);codes.add(336000);codes.add(337000);codes.add(338000);codes.add(3390); 
		r213 = getData(codes, ts, "cr", "");	r213p = getData(codes, tsBeginOfYear, "cr", "");	
	
		codes="342000,343000,344000";//.clear(); codes.add(3400);
		r214 = getData(codes, ts, "cr", "");	r214p = getData(codes, tsBeginOfYear, "cr", "");

		codes="311000";//.clear(); codes.add(311000);
		r215 = getData(codes, ts, "cr", "");	r215p = getData(codes, tsBeginOfYear, "cr", "");

		codes="335000";//.clear(); codes.add(335000);
		r216 = getData(codes, ts, "cr", "");	r216p = getData(codes, tsBeginOfYear, "cr", "");

		codes="3500,312000,313000,314000,315000,316000,317000,318000,319000,3200,341000";//.clear(); codes.add(3500);codes.add(312000);codes.add(313000);codes.add(314000);codes.add(315000);codes.add(316000);codes.add(317000);codes.add(318000);codes.add(319000);codes.add(3200);
		r217 = getData(codes, ts, "cr", "");	r217p = getData(codes, tsBeginOfYear, "cr", "");

		
		
		r300 = r300.add(r210).add(r212).add(r213).add(r214).add(r215).add(r216).add(r217);
		r300p = r300p.add(r210p).add(r212p).add(r213p).add(r214p).add(r215p).add(r216p).add(r217p);
//		codes="301000,302000,303000,304000,305000,331000,332000,333000,334000,336000,337000,338000,3390,3400,311000,335000,3500,312000,313000,314000,315000,316000,317000,318000,319000,3200";
//		r300 = getData(codes, ts, "cr", "");	r300p = getData(codes, tsBeginOfYear, "cr", "");
		
		
		codes="401000,402000";//.clear(); codes.add(401000);codes.add(402000);
		r310 = getData(codes, ts, "cr", "");	r310p = getData(codes, tsBeginOfYear, "cr", "");

		codes="403000";//.clear(); codes.add(403000);
		r312 = getData(codes, ts, "cr", "");	r312p = getData(codes, tsBeginOfYear, "cr", "");

		codes="4100,4170";//.clear(); codes.add(4100); codes.add(4170);
		r313 = getData(codes, ts, "cr", "");	r313p = getData(codes, tsBeginOfYear, "cr", "");

		codes="4200";//.clear(); codes.add(4200);
		r314 = getData(codes, ts, "cr", "");	r314p = getData(codes, tsBeginOfYear, "cr", "");

		codes="4300";//.clear(); codes.add(4300);
		r315 = getData(codes, ts, "cr", "");	r315p = getData(codes, tsBeginOfYear, "cr", "");

		codes="4400";//.clear(); codes.add(4400);
		r316 = getData(codes, ts, "cr", "");	r316p = getData(codes, tsBeginOfYear, "cr", "");

		codes="5000";//.clear(); codes.add(5000);
		r410 = getData(codes, ts, "cr", "");	r410p = getData(codes, tsBeginOfYear, "cr", "");

		codes="5100";//.clear(); codes.add(5100);
		r411 = getData(codes, ts, "cr", "");	r411p = getData(codes, tsBeginOfYear, "cr", "");

		codes="5200";//.clear(); codes.add(5200);
		r412 = getData(codes, ts, "cr", "");	r412p = getData(codes, tsBeginOfYear, "cr", "");

		codes="5300";//.clear(); codes.add(5300);
		r413 = getData(codes, ts, "cr", "");	r413p = getData(codes, tsBeginOfYear, "cr", "");

		codes="5400";//.clear(); codes.add(5400);
//		r414 = getData(codes, ts, "cr", "");	
		r414p = getData(codes, tsBeginOfYear, "cr", "");
		
		BigDecimal v3030 = getData2("303", tsStartDate, tsEndDate, "cr").negate();
		
		BigDecimal v541000 = getData7(tsStartDate, null, "541,543"); 
		
		BigDecimal v6000 = getData2("6", tsStartDate, tsEndDate, "cr");
		BigDecimal v7000 = getData2("7", tsStartDate, tsEndDate, "dr");
		BigDecimal v6_7 = v6000.subtract(v7000);
		
		r414 = r414.add(v3030).add(v541000).add(v6_7);
		
		//write to list-----------------------------------------------------------------
		BigDecimal tmp = zero;
		BigDecimal tmpp = zero;
		Line line;
		/**/		line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);	
		/*	010	*/	line = new Line();		line.setValue1(getDiv1000(r010).toString());	line.setValue2(getDiv1000(r010p).toString());	list.add(line);
		/*	011	*/	line = new Line();		line.setValue1(getDiv1000(r011).toString());	line.setValue2(getDiv1000(r011p).toString());	list.add(line);
		/*	012	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	013	*/	line = new Line();		line.setValue1(getDiv1000(r013).toString());	line.setValue2(getDiv1000(r013p).toString());	list.add(line);
		/*	014	*/	line = new Line();		line.setValue1(getDiv1000(r014).toString());	line.setValue2(getDiv1000(r014p).toString());	list.add(line);
		/*	015	*/	line = new Line();		line.setValue1(getDiv1000(r015).toString());	line.setValue2(getDiv1000(r015p).toString());	list.add(line);
		/*	016	*/	line = new Line();		line.setValue1(getDiv1000(r016).toString());	line.setValue2(getDiv1000(r016p).toString());	list.add(line);
		/*	017	*/	line = new Line();		line.setValue1(getDiv1000(r017).toString());	line.setValue2(getDiv1000(r017p).toString());	list.add(line);
		/*	018	*/	line = new Line();		line.setValue1(getDiv1000(r018).toString());	line.setValue2(getDiv1000(r018p).toString());	list.add(line);
		/*	019	*/	line = new Line();		line.setValue1(getDiv1000(r019).toString());	line.setValue2(getDiv1000(r019p).toString());	list.add(line);
		
		r100 = r100.add(r010).add(r011).add(r013).add(r014).add(r015).add(r016).add(r017).add(r018).add(r019);
		r100p = r100p.add(r010p).add(r011p).add(r013p).add(r014p).add(r015p).add(r016p).add(r017p).add(r018p).add(r019p);
		/*	100	*/	line = new Line();		line.setValue1(getDiv1000(r100).toString());	line.setValue2(getDiv1000(r100p).toString());	list.add(line);
		
		/*	101	*/	line = new Line();		line.setValue1(getDiv1000(r101).toString());	line.setValue2(getDiv1000(r101p).toString());	list.add(line);
		
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		
		/*	110	*/	line = new Line();		line.setValue1(getDiv1000(r110).toString());	line.setValue2(getDiv1000(r110p).toString());	list.add(line);
		/*	111	*/	line = new Line();		line.setValue1(getDiv1000(r111).toString());	line.setValue2(getDiv1000(r111p).toString());	list.add(line);
		/*	112	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	113	*/	line = new Line();		line.setValue1(getDiv1000(r113).toString());	line.setValue2(getDiv1000(r113p).toString());	list.add(line);
		/*	114	*/	line = new Line();		line.setValue1(getDiv1000(r114).toString());	line.setValue2(getDiv1000(r114p).toString());	list.add(line);
		/*	115	*/	line = new Line();		line.setValue1(getDiv1000(r115).toString());	line.setValue2(getDiv1000(r115p).toString());	list.add(line);
		/*	116	*/	line = new Line();		line.setValue1(getDiv1000(r116).toString());	line.setValue2(getDiv1000(r116p).toString());	list.add(line);
		/*	117	*/	line = new Line();		line.setValue1(getDiv1000(r117).toString());	line.setValue2(getDiv1000(r117p).toString());	list.add(line);
		/*	118	*/	line = new Line();		line.setValue1(getDiv1000(r118).toString());	line.setValue2(getDiv1000(r118p).toString());	list.add(line);
		/*	119	*/	line = new Line();		line.setValue1(getDiv1000(r119).toString());	line.setValue2(getDiv1000(r119p).toString());	list.add(line);
		/*	120	*/	line = new Line();		line.setValue1(getDiv1000(r120).toString());	line.setValue2(getDiv1000(r120p).toString());	list.add(line);
		/*	121	*/	line = new Line();		line.setValue1(getDiv1000(r121).toString());	line.setValue2(getDiv1000(r121p).toString());	list.add(line);
		/*	122	*/	line = new Line();		line.setValue1(getDiv1000(r122).toString());	line.setValue2(getDiv1000(r122p).toString());	list.add(line);
		/*	123	*/	line = new Line();		line.setValue1(getDiv1000(r123).toString());	line.setValue2(getDiv1000(r123p).toString());	list.add(line);
		
		
//		r200 = r200.add(r110).add(r111).add(r113).add(r114).add(r115).add(r116).add(r117).add(r118).add(r119).add(r120).add(r121).add(r122).add(r123);
//		r200p = r200p.add(r110p).add(r111p).add(r113p).add(r114p).add(r115p).add(r116p).add(r117p).add(r118p).add(r119p).add(r120p).add(r121p).add(r122p).add(r123p);
		/*	200	*/	line = new Line();		line.setValue1(getDiv1000(r200).toString());	line.setValue2(getDiv1000(r200p).toString());	list.add(line);
		
		tmp = zero; tmpp = zero;
		tmp = tmp.add(r100).add(r101).add(r200);
		tmpp = tmpp.add(r100p).add(r101p).add(r200p);
		/* 100 + 101 + 200 */	line = new Line();		line.setValue1(getDiv1000(tmp).toString());	line.setValue2(getDiv1000(tmpp).toString());	list.add(line);
		
//		/* 	 */	line = new Line();		line.setValue1("На конец отчетного периода");	line.setValue2("На начало отчетного периода");	list.add(line);
		/*	 */	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	210	*/	line = new Line();		line.setValue1(getDiv1000(r210).toString());	line.setValue2(getDiv1000(r210p).toString());	list.add(line);
		/*	211	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	212	*/	line = new Line();		line.setValue1(getDiv1000(r212).toString());	line.setValue2(getDiv1000(r212p).toString());	list.add(line);
		/*	213	*/	line = new Line();		line.setValue1(getDiv1000(r213).toString());	line.setValue2(getDiv1000(r213p).toString());	list.add(line);
		/*	214	*/	line = new Line();		line.setValue1(getDiv1000(r214).toString());	line.setValue2(getDiv1000(r214p).toString());	list.add(line);
		/*	215	*/	line = new Line();		line.setValue1(getDiv1000(r215).toString());	line.setValue2(getDiv1000(r215p).toString());	list.add(line);
		/*	216	*/	line = new Line();		line.setValue1(getDiv1000(r216).toString());	line.setValue2(getDiv1000(r216p).toString());	list.add(line);
		/*	217	*/	line = new Line();		line.setValue1(getDiv1000(r217).toString());	line.setValue2(getDiv1000(r217p).toString());	list.add(line);
		
		
//		r300 = r300.add(r210).add(r212).add(r213).add(r214).add(r215).add(r216).add(r217);
//		r300p = r300p.add(r210p).add(r212p).add(r213p).add(r214p).add(r215p).add(r216p).add(r217p);
		/* 	300 */	line = new Line();		line.setValue1(getDiv1000(r300).toString());	line.setValue2(getDiv1000(r300p).toString());	list.add(line);
		/*	301	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	310	*/	line = new Line();		line.setValue1(getDiv1000(r310).toString());	line.setValue2(getDiv1000(r310p).toString());	list.add(line);
		/*	311	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	312	*/	line = new Line();		line.setValue1(getDiv1000(r312).toString());	line.setValue2(getDiv1000(r312p).toString());	list.add(line);
		/*	313	*/	line = new Line();		line.setValue1(getDiv1000(r313).toString());	line.setValue2(getDiv1000(r313p).toString());	list.add(line);
		/*	314	*/	line = new Line();		line.setValue1(getDiv1000(r314).toString());	line.setValue2(getDiv1000(r314p).toString());	list.add(line);
		/*	315	*/	line = new Line();		line.setValue1(getDiv1000(r315).toString());	line.setValue2(getDiv1000(r315p).toString());	list.add(line);
		/*	316	*/	line = new Line();		line.setValue1(getDiv1000(r316).toString());	line.setValue2(getDiv1000(r316p).toString());	list.add(line);
		
		
		r400 = r400.add(r310).add(r312).add(r313).add(r314).add(r315).add(r316);
		r400p = r400p.add(r310p).add(r312p).add(r313p).add(r314p).add(r315p).add(r316p);
		/*	400	*/	line = new Line();		line.setValue1(getDiv1000(r400).toString());	line.setValue2(getDiv1000(r400p).toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	410	*/	line = new Line();		line.setValue1(getDiv1000(r410).toString());	line.setValue2(getDiv1000(r410p).toString());	list.add(line);
		/*	411	*/	line = new Line();		line.setValue1(getDiv1000(r411).toString());	line.setValue2(getDiv1000(r411p).toString());	list.add(line);
		/*	412	*/	line = new Line();		line.setValue1(getDiv1000(r412).toString());	line.setValue2(getDiv1000(r412p).toString());	list.add(line);
		/*	413	*/	line = new Line();		line.setValue1(getDiv1000(r413).toString());	line.setValue2(getDiv1000(r413p).toString());	list.add(line);
		/*	414	*/	line = new Line();		line.setValue1(getDiv1000(r414).toString());	line.setValue2(getDiv1000(r414p).toString());	list.add(line);
		
		r420 = r420.add(r410).add(r411).add(r412).add(r413).add(r414);
		r420p = r420p.add(r410p).add(r411p).add(r412p).add(r413p).add(r414p);
		/*	420	*/	line = new Line();		line.setValue1(getDiv1000(r420).toString());	line.setValue2(getDiv1000(r420p).toString());	list.add(line);
		/*	421	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		
		r500 = r500.add(r420);
		r500p = r500p.add(r420p);
		/*	500	*/	line = new Line();		line.setValue1(getDiv1000(r500).toString());	line.setValue2(getDiv1000(r500p).toString());	list.add(line);
		
		tmp = zero; tmpp = zero;
		tmp = tmp.add(r300).add(r400).add(r500);
		tmpp = tmpp.add(r300p).add(r400p).add(r500p);
		/*		*/	line = new Line();		line.setValue1(getDiv1000(tmp).toString());	line.setValue2(getDiv1000(tmpp).toString());	list.add(line);

		return list;

	}

	
	private BigDecimal getData(String codes, Timestamp dateTo, String tt, String bp){
		BigDecimal result = zero;
		StringBuffer sql = new StringBuffer();
		String s = "";
		if(!bp.equals(""))
			s = " and tbl1.c_bpartner_id = " + bp;
//		String whereCodes = "";
//		if(codes.size() > 0){
//			whereCodes = " and ( ";
//			for(int i=0; i < codes.size(); i++){
//				if(i == (codes.size()-1))
//					whereCodes += "	ce.value like '"+codes.get(i)+"%'	"; 
//				else
//					whereCodes += "	ce.value like '"+codes.get(i)+"%' or ";
//					
//			}
//			whereCodes += " ) ";
//				
//		}

//		//prepare sql
//		sql.append(
//				"	select coalesce(sum(fa.amtacctdr - fa.amtacctcr),0)		\n" + 
//				"	from adempiere.fact_acct fa, adempiere.c_elementvalue ce	\n"+	  
//				"	where fa.account_id = ce.c_elementvalue_id		\n"+
//				"	and PostingType='A'		\n"+		  
//				"	and fa.dateacct < '"+ dateTo +"'		\n" 
////				"	and (ce.value like '1000%' or ce.value like '1030%' or ce.value like '1040%' or ce.value like '1050%')	\n"
//				).append(whereCodes)
//				;
//		
////		String s = "select	row_010	from(select (select amtacctdr from \"FI_balans_sheet_row_sum\"('{1030,1030,1040,1050}','1900-01-01'::date,'Mon Sep 30 00:00:00 ALMT 2013'::date,'dr'))::numeric as row_010) as temp1";
//		
		sql.append(
//				"select case when '"+tt+"'='dr' and sum(tbl1.amtacctdr)-sum(tbl1.amtacctcr) is not null then sum(tbl1.amtacctdr)-sum(tbl1.amtacctcr)  \n" +
//						"       else 0::numeric(1,0)\n" +
//						"       end as amtacctdr,\n" +
//						"       case when '"+tt+"'='cr' and sum(tbl1.amtacctcr)-sum(tbl1.amtacctdr) is not null then sum(tbl1.amtacctcr)-sum(tbl1.amtacctdr)  \n" +
//						"       else 0::numeric(1,0)\n" +
//						"       end as amtacctcr\n" +
//						"from        \n" +
//						"(\n" +
//						"with recursive temp1 (ad_tree_id, node_id, value, parent_id, PATH, PATHVALUE, LEVEL, CYCLE) as (\n" +
//						"select t1.ad_tree_id, t1.node_id, cev1.value, t1.parent_id, cast(array[t1.node_id] as numeric(10,0)[]), cast(array[cev1.value] as character varying(40)[]), 1, FALSE\n" +
//						"from adempiere.ad_treenode as t1\n" +
//						"left join adempiere.c_elementvalue cev1 on cev1.ad_org_id = t1.ad_org_id and cev1.ad_client_id = t1.ad_client_id and cev1.c_elementvalue_id = t1.node_id\n" +
//						"union all\n" +
//						"select t2.ad_tree_id, t2.node_id, cev2.value, t2.parent_id, cast(temp1.PATH || t2.node_id as numeric(10,0) []), cast(temp1.PATHVALUE || cev2.value as character varying(40) []),LEVEL+1, t2.node_id = any(temp1.PATH)\n" +
//						"from adempiere.ad_treenode as t2\n" +
//						"left join adempiere.c_elementvalue cev2 on cev2.ad_org_id = t2.ad_org_id and cev2.ad_client_id = t2.ad_client_id and cev2.c_elementvalue_id = t2.node_id\n" +
//						"inner join temp1 on (temp1.node_id = t2.parent_id) and not CYCLE\n" +
//						")\n" +
//						"select temp1.*, fa.dateacct, fa.amtacctdr, fa.amtacctcr, fa.c_bpartner_id, cbp.c_bp_group_id, cbpg.name from temp1\n" +
//						"inner join adempiere.fact_acct as fa on fa.account_id = any(temp1.path)\n" +
//						"left join adempiere.c_bpartner cbp on cbp.ad_org_id = fa.ad_org_id and cbp.ad_client_id = fa.ad_client_id and cbp.c_bpartner_id = fa.c_bpartner_id\n" +
//						"left join adempiere.c_bp_group cbpg on cbpg.ad_org_id = cbp.ad_org_id and cbpg.ad_client_id = cbp.ad_client_id and cbpg.c_bp_group_id = cbp.c_bp_group_id\n" +
//						"where temp1.path[1] in (select c_elementvalue_id from adempiere.c_elementvalue where value = any('{"+codes+"}'))\n" +
//						"  and ((   ('127000' = any(temp1.pathvalue) and '127000_16' = any('{"+codes+"}') and cbpg.name like 'Связанные стороны')\n" +
//						"       or ('127000' = any(temp1.pathvalue) and '127000_15' = any('{"+codes+"}') and cbpg.name not like 'Связанные стороны')) \n" +
//						"       or ('127000' <> all(temp1.pathvalue)))\n" +
//						"order by pathvalue       \n" +
//						") as tbl1\n" +
//						"where tbl1.dateacct between '1900-01-01' and '"+ dateTo +"'"
				
"select case when '"+tt+"'='dr' and sum(tbl1.amtacctdr)-sum(tbl1.amtacctcr) is not null then sum(tbl1.amtacctdr)-sum(tbl1.amtacctcr)  \n" +
"       else 0::numeric(1,0)\n" +
"       end as amtacctdr,\n" +
"       case when '"+tt+"'='cr' and sum(tbl1.amtacctcr)-sum(tbl1.amtacctdr) is not null then sum(tbl1.amtacctcr)-sum(tbl1.amtacctdr)  \n" +
"       else 0::numeric(1,0)\n" +
"       end as amtacctcr\n" +
"from        \n" +
"(\n" +
"with recursive temp1 (ad_tree_id, node_id, value, parent_id, PATH, PATHVALUE, LEVEL, CYCLE) as (\n" +
"select t1.ad_tree_id\n" +
"     , t1.node_id\n" +
"     , cev1.value\n" +
"     , t1.parent_id\n" +
"     , cast(array[t1.node_id] as numeric(10,0)[])\n" +
"     , cast(array[cev1.value] as character varying(40)[])\n" +
"     , 1\n" +
"     , FALSE\n" +
"from adempiere.ad_treenode as t1\n" +
"left join adempiere.c_elementvalue cev1 \n" +
"       on cev1.ad_org_id = t1.ad_org_id \n" +
"      and cev1.ad_client_id = t1.ad_client_id \n" +
"      and cev1.c_elementvalue_id = t1.node_id\n" +
"where cev1.value is not null\n" +
"union all\n" +
"select t2.ad_tree_id\n" +
"     , t2.node_id\n" +
"     , cev2.value\n" +
"     , t2.parent_id\n" +
"     , cast(temp1.PATH || t2.node_id as numeric(10,0) [])\n" +
"     , cast(temp1.PATHVALUE || cev2.value as character varying(40) [])\n" +
"     , LEVEL+1\n" +
"     , t2.node_id = any(temp1.PATH)\n" +
"from adempiere.ad_treenode as t2\n" +
"left join adempiere.c_elementvalue cev2 \n" +
"       on cev2.ad_org_id = t2.ad_org_id \n" +
"      and cev2.ad_client_id = t2.ad_client_id \n" +
"      and cev2.c_elementvalue_id = t2.node_id\n" +
"inner join temp1 \n" +
"        on (temp1.node_id = t2.parent_id) \n" +
"       and not CYCLE\n" +
"       and temp1.ad_tree_id = t2.ad_tree_id\n" +
"where cev2.value is not null\n" +
")\n" +
"select temp1.*\n" +
"     , fa.dateacct\n" +
"     , fa.amtacctdr\n" +
"     , fa.amtacctcr\n" +
"     , fa.c_bpartner_id\n" +
"     , cbp.c_bp_group_id\n" +
"     , cbpg.name from temp1\n" +
"inner join adempiere.fact_acct as fa \n" +
"        on fa.account_id = any(temp1.path)\n" +
"left join adempiere.c_bpartner cbp \n" +
"       on cbp.ad_org_id = fa.ad_org_id \n" +
"      and cbp.ad_client_id = fa.ad_client_id \n" +
"      and cbp.c_bpartner_id = fa.c_bpartner_id	\n" +
"left join adempiere.c_bp_group cbpg \n" +
"       on cbpg.ad_org_id = cbp.ad_org_id \n" +
"      and cbpg.ad_client_id = cbp.ad_client_id \n" +
"      and cbpg.c_bp_group_id = cbp.c_bp_group_id\n" +
"where temp1.path[1] in ( select c_elementvalue_id \n" +
"                         from adempiere.c_elementvalue \n" +
"                         where value = any('{"+codes+"}'))\n" +
"  and ((('127000' = any(temp1.pathvalue) and '127000_16' = any('{"+codes+"}') and cbpg.name like 'Связанные стороны')\n" +
"     or ('127000' = any(temp1.pathvalue) and '127000_15' = any('{"+codes+"}') and cbpg.name not like 'Связанные стороны')) \n" +
"     or ('127000' <> all(temp1.pathvalue)))\n" +
"    and temp1.ad_tree_id = 1000008     \n" +
"order by pathvalue       \n" +
") as tbl1\n" +
"where tbl1.dateacct between '1900-01-01' and '"+ dateTo +"' " + s				
				);
		
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			pstmt = DB.prepareStatement(sql.toString(), null);
//			pstmt = DB.prepareStatement(s, null);
//			System.out.println(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				if(tt.equals("dr"))
					result = rs.getBigDecimal(1);
				else
					result = rs.getBigDecimal(2);
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
		
//		if(result.compareTo(zero) != 0 )
//			result = result.divide(new BigDecimal(1000));
//		
//		result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
		
		return result; 
	}
	
	//Intercompany Sales1
	private List<Line> loadICS1(MBCSFRInputFormCompany company, MBCSFRInputForm form) {
		List<Line> list = new ArrayList<Line>();
		
		MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
		int C_Period_ID = period.getPeriodTo();
		MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);

		Date d = p.getEndDate();
		
		List<MBCSFRInputFormLine> lines = getMBCSFRInputFormLines(form);
		
		List<MBCSFRInputFormColumn> columns = getMBCSFRInputFormColumn(lines); 
		
		int pos = 1;
		for(MBCSFRInputFormLine line: lines){
			String codes = "";
			
			switch (pos++){
			case 1:
				codes = "1200,1250,1280,129000";
				break;
			case 2:
				codes = "2100,2150,2180";
				break;
			case 3:
				codes = "331000,332000,333000,334000,336000,337000,338000,339000";
				break;
			case 4:
				codes = "4100,4170";
				break;				
			}
			Line l = new Line();
			BigDecimal  summary = zero;
			int col = 0;
			for(MBCSFRInputFormColumn column: columns){
				if(column.getBCS_FRInputFormLine_ID() == line.get_ID()){
					col++;
					
					if(column.getC_BPartner_ID() != 0){
						BigDecimal v = getDataICS1(null, d, codes, column.getC_BPartner_ID());
						summary = summary.add(v);
						switch(col){
							case 1:
								l.setValue1(getDiv1000(v).toString());
								break;
							case 2:
								l.setValue2(getDiv1000(v).toString());
								break;
							case 3:
								l.setValue3(getDiv1000(v).toString());
								break;
							case 4:
								l.setValue4(getDiv1000(v).toString());
								break;
							case 5:
								l.setValue5(getDiv1000(v).toString());
								break;
							case 6:
								l.setValue6(getDiv1000(v).toString());
								break;
							case 7:
								l.setValue7(getDiv1000(v).toString());
								break;
							case 8:
								l.setValue8(getDiv1000(v).toString());
								break;
								
						}
						
						
					}
					
				}
			}
			l.setValue1(getDiv1000(summary).toString());
			list.add(l);
			
			
		}
		
//		getMBCSFRInputFormLine(forms);
		
		
		
		return list;
	}
	
	
	private BigDecimal getDataICS1(Date startDate, Date endDate, String codes, int bp) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		BigDecimal result = new BigDecimal(0);
		
		String[] words = codes.split(",");  
		
		String whereCodes = "";
		if(words.length > 0){
			whereCodes = " and ( ";
			for(int i=0; i < words.length; i++){
				if(i == (words.length-1))
					whereCodes += "	ce.value like '"+words[i]+"%'	"; 
				else
					whereCodes += "	ce.value like '"+words[i]+"%' or ";
					
			}
			whereCodes += " ) ";
				
		}

		
		String sql = 
				  
				  "			select coalesce(sum(fa.amtacctcr - fa.amtacctdr),0)	\n"
				+ "			from fact_acct fa, c_elementvalue ce 	\n"
				+ "			where fa.account_id = ce.c_elementvalue_id 	\n"
				+ "			and fa.c_bpartner_id =  " + bp
				;

		if(startDate != null && endDate != null){	
			sql +=	"		and fa.dateacct between '"+startDate+"'::date and '"+endDate+"'::date	\n"	;
		}else if(endDate == null){
			sql +=		"		and fa.dateacct <= '"+startDate+"'::date 	\n"	;
		}if(startDate == null){
			sql +=		"		and fa.dateacct <= '"+endDate+"'::date	\n"	;
		}
		
			sql += whereCodes;				

		

		try {
			pstmt = DB.prepareStatement(sql, null);

			rs = pstmt.executeQuery();
			if (rs.next()) {
				result = rs.getBigDecimal(1);
//				if(result.compareTo(Env.ZERO) != 0){
//						result = result.divide(new BigDecimal(1000));
//						result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
//				}
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "Report: FI_Form4", e);
			return new BigDecimal(0);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
		return result;
		
	}
	

	private List<Line> load2(MBCSFRInputFormCompany company) {
 		List<Line> list = new ArrayList<Line>();
		
		MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
		int C_Period_ID = period.getPeriodTo();
		MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
		Long time;

		Timestamp ts1 = p.getEndDate();//begin of choosen period 
		//ts2
		Timestamp ts2 = p.getEndDate();//choosen period 
		Timestamp ts3 = p.getEndDate();//begin of choosen period - year 
		Timestamp ts4 = p.getEndDate();//choosen period - year
		
		//ts1
		Date d2 = new Date(ts2.getTime());
		Calendar c = Calendar.getInstance();
		c.setTime(new Date(ts2.getTime()));
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.MONTH, 0);
		Date d1 = new Date(c.getTimeInMillis());

		//ts3
		c.setTime(d1);
		c.add(Calendar.YEAR, -1);
		Date d3 = new Date(c.getTimeInMillis());
		
		//ts4
		c.setTime(p.getEndDate());
		c.add(Calendar.YEAR, -1);
		Date d4 = new Date(c.getTimeInMillis());
		
		
		
		String codes = "";
		
		BigDecimal r010 = zero; BigDecimal r010p = zero;
		BigDecimal r011 = zero; BigDecimal r011p = zero;
		BigDecimal r012 = zero; BigDecimal r012p = zero;
		BigDecimal r013 = zero; BigDecimal r013p = zero;
		BigDecimal r014 = zero; BigDecimal r014p = zero;
		BigDecimal r015 = zero; BigDecimal r015p = zero;
		BigDecimal r016 = zero; BigDecimal r016p = zero;
		BigDecimal r020 = zero; BigDecimal r020p = zero;
		BigDecimal r021 = zero; BigDecimal r021p = zero;
		BigDecimal r022 = zero; BigDecimal r022p = zero;
		BigDecimal r023 = zero; BigDecimal r023p = zero;
		BigDecimal r100 = zero; BigDecimal r100p = zero;
		BigDecimal r101 = zero; BigDecimal r101p = zero;
		BigDecimal r200 = zero; BigDecimal r200p = zero;
		BigDecimal r300 = zero; BigDecimal r300p = zero;
		BigDecimal r500 = zero; BigDecimal r500p = zero;
		
		codes = "601,602,603";
		r010 = getData2(codes, d1, d2, "cr");	r010p = getData2(codes, d3, d4, "cr");
		
		codes = "701";
		r011 = getData2(codes, d1, d2, "cr");	r011p = getData2(codes, d3, d4, "cr");

		r012 = r012.add(r010).add(r011);		r012p = r012.add(r010p).add(r011p);
		
		codes = "711";
		r013 = getData2(codes, d1, d2, "dr");	r013p = getData2(codes, d3, d4, "dr");
		
		codes = "721";
		r014 = getData2(codes, d1, d2, "dr");	r014p = getData2(codes, d3, d4, "dr");

		codes = "741,742,743,744,745,746,747";
		r015 = getData2(codes, d1, d2, "cr");	r015p = getData2(codes, d3, d4, "cr");
		
		codes = "621,622,623,624,625,626,627,627,628";
		r016 = getData2(codes, d1, d2, "cr");	r016p = getData2(codes, d3, d4, "cr");
		
		r020 = r020.subtract(r012).subtract(r013).subtract(r014).subtract(r015).add(r016);		r020p = r020p.subtract(r012p).subtract(r013p).subtract(r014p).subtract(r015p).add(r016p);

		codes = "611,612,613,614,615,616";
		r021 = getData2(codes, d1, d2, "cr");	r021p = getData2(codes, d3, d4, "cr");

		codes = "731,732,733,734";
		r022 = getData2(codes, d1, d2, "cr");	r022p = getData2(codes, d3, d4, "cr");
		
		codes = "641,642,761,762";
		r023 = getData2(codes, d1, d2, "cr");	r023p = getData2(codes, d3, d4, "cr");

		r100 = r100.add(r020).add(r021).add(r022).add(r023);	r100p = r100p.add(r020p).add(r021p).add(r022p).add(r023p);
		
		codes = "771";
		r101 = getData2(codes, d1, d2, "dr");	r101p = getData2(codes, d3, d4, "dr");		
		
		r200 = r200.add(r100).subtract(r101);	r200p = r200p.add(r100p).subtract(r101p);
		
		r300 = r300.add(r200);	r300p = r300p.add(r200p);
		
		r500 = r500.add(r300);	r500p = r500p.add(r300p);
		

		Line line;

		/*	010	*/	line = new Line();		line.setValue1(getDiv1000(r010).toString());	line.setValue2(getDiv1000(r010p).toString());	list.add(line);
		/*	011	*/	line = new Line();		line.setValue1(getDiv1000(r011).toString());	line.setValue2(getDiv1000(r011p).toString());	list.add(line);
		/*	012	*/	line = new Line();		line.setValue1(getDiv1000(r012).toString());	line.setValue2(getDiv1000(r012p).toString());	list.add(line);
		/*	013	*/	line = new Line();		line.setValue1(getDiv1000(r013).toString());	line.setValue2(getDiv1000(r013p).toString());	list.add(line);
		/*	014	*/	line = new Line();		line.setValue1(getDiv1000(r014).toString());	line.setValue2(getDiv1000(r014p).toString());	list.add(line);
		/*	015	*/	line = new Line();		line.setValue1(getDiv1000(r015).toString());	line.setValue2(getDiv1000(r015p).toString());	list.add(line);
		/*	016	*/	line = new Line();		line.setValue1(getDiv1000(r016).toString());	line.setValue2(getDiv1000(r016p).toString());	list.add(line);
		
		/*	020	*/	line = new Line();		line.setValue1(getDiv1000(r020).toString());	line.setValue2(getDiv1000(r020p).toString());	list.add(line);
		/*	021	*/	line = new Line();		line.setValue1(getDiv1000(r021).toString());	line.setValue2(getDiv1000(r021p).toString());	list.add(line);
		/*	022	*/	line = new Line();		line.setValue1(getDiv1000(r022).toString());	line.setValue2(getDiv1000(r022p).toString());	list.add(line);
		/*	023	*/	line = new Line();		line.setValue1(getDiv1000(r023).toString());	line.setValue2(getDiv1000(r023p).toString());	list.add(line);
		/*	024	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	025	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		
		/*	100	*/	line = new Line();		line.setValue1(getDiv1000(r100).toString());	line.setValue2(getDiv1000(r100p).toString());	list.add(line);
		/*	101	*/	line = new Line();		line.setValue1(getDiv1000(r101).toString());	line.setValue2(getDiv1000(r101p).toString());	list.add(line);
		
		/*	200	*/	line = new Line();		line.setValue1(getDiv1000(r200).toString());	line.setValue2(getDiv1000(r200p).toString());	list.add(line);
		/*	201	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		
		
		/*	300	*/	line = new Line();		line.setValue1(getDiv1000(r300).toString());	line.setValue2(getDiv1000(r300p).toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		
		/*	400	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	410	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	411	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	412	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	413	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	414	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	415	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	416	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	417	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	418	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	419	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	420	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		
		/*	500	*/	line = new Line();		line.setValue1(getDiv1000(r500).toString());	line.setValue2(getDiv1000(r500p).toString());	list.add(line);
		/*	501	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	502	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	503	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		
		/*	600	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	601	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	602	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	603	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	604	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	605	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	606	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);

		return list;

	}

	
	private BigDecimal getData2(String codes, Date dateFrom, Date dateTo, String type){
		BigDecimal result = zero;
		StringBuffer sql = new StringBuffer();
		
		String[] words = codes.split(",");  
		
		String whereCodes = "";
		if(words.length > 0){
			whereCodes = " and ( ";
			for(int i=0; i < words.length; i++){
				if(i == (words.length-1))
					whereCodes += "	ce.value like '"+words[i]+"%'	"; 
				else
					whereCodes += "	ce.value like '"+words[i]+"%' or ";
					
			}
			whereCodes += " ) ";
				
		}

//		//prepare sql
		if(whereCodes.equals(""))
			return result;
		
		if(type.equals("dr"))
			if(codes.equals("50"))
				sql.append("	select coalesce(sum(fa.amtacctcr - fa.amtacctdr),0)	\n");
			else	
				sql.append("	select coalesce(sum(fa.amtacctdr),0)	\n");
		else
			sql.append("	select coalesce(sum(fa.amtacctcr),0)	\n");
			
		sql.append(
				"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n"	+ 
				"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	+	
				"		" + whereCodes
				);
		
		if(codes.equals("541000") || codes.equals("543000"))
			sql.append(
			 "				group by fa.dateacct " 			
			+ "				order by fa.dateacct " 
			+ " 			OFFSET 0 FETCH FIRST 1 ROWS ONLY		" 
		);
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			pstmt = DB.prepareStatement(sql.toString(), null);
//			pstmt = DB.prepareStatement(s, null);
//			System.out.println(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
					result = rs.getBigDecimal(1);
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
		
//		if(result.compareTo(zero) != 0 )
//			result = result.divide(new BigDecimal(1000));
		
//		result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
		
		return result; 
	}

	protected BigDecimal getDiv1000(BigDecimal value){
		BigDecimal result = zero;
		
		if(value != null && value.compareTo(zero) != 0 ){
			result = value.divide(new BigDecimal(1000));
			result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
		}

		return result;
	}
	
	
	//Intercompany Sales1
	private List<Line> loadICS2(MBCSFRInputFormCompany company, MBCSFRInputForm form) {
		List<Line> list = new ArrayList<Line>();
		
		MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
		int C_Period_ID = period.getPeriodFrom();
		MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
		Date d = p.getStartDate();

		int C_Period_ID2 = period.getPeriodTo();
		MPeriod p2 = new MPeriod(Env.getCtx(), C_Period_ID2, null);
		Date d2 = p2.getEndDate();
		
		List<MBCSFRInputFormLine> lines = getMBCSFRInputFormLines(form);
		
		List<MBCSFRInputFormColumn> columns = getMBCSFRInputFormColumn(lines); 
		
		int pos = 1;
		for(MBCSFRInputFormLine line: lines){
			String codes = "";
			
			switch (pos++){
			case 1:
				codes = "601,602,603";
				break;
			case 2:
				codes = "701";
				break;
			case 3:
				codes = "711";
				break;
			case 4:
				codes = "721";
				break;
			case 5:
				codes = "741,742,743,744,745,746,747";
				break;				
			case 6:
				codes = "621,622,623,624,625,626,627,627,628";
				break;				
				
			}
			Line l = new Line();
			BigDecimal  summary = zero;
			int col = 0;
			for(MBCSFRInputFormColumn column: columns){
				if(column.getBCS_FRInputFormLine_ID() == line.get_ID()){
					col++;
					
					if(column.getC_BPartner_ID() != 0){
						BigDecimal v = getDataICS2(d, d2, codes, column.getC_BPartner_ID()).abs();
						summary = summary.add(v);
						switch(col){
							case 1:
								l.setValue1(getDiv1000(v).toString());
								break;
							case 2:
								l.setValue2(getDiv1000(v).toString());
								break;
							case 3:
								l.setValue3(getDiv1000(v).toString());
								break;
							case 4:
								l.setValue4(getDiv1000(v).toString());
								break;
							case 5:
								l.setValue5(getDiv1000(v).toString());
								break;
							case 6:
								l.setValue6(getDiv1000(v).toString());
								break;
							case 7:
								l.setValue7(getDiv1000(v).toString());
								break;
							case 8:
								l.setValue8(getDiv1000(v).toString());
								break;
								
						}
						
						
					}
					
				}
			}
			l.setValue1(getDiv1000(summary).toString());
			list.add(l);
			
			
		}
		
//		getMBCSFRInputFormLine(forms);
		
		
		
		return list;
	}
	
	
	private BigDecimal getDataICS2(Date startDate, Date endDate, String codes, int bp) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		BigDecimal result = new BigDecimal(0);
		
		String[] words = codes.split(",");  
		
		String whereCodes = "";
		if(words.length > 0){
			whereCodes = " and ( ";
			for(int i=0; i < words.length; i++){
				if(i == (words.length-1))
					whereCodes += "	ce.value like '"+words[i]+"%'	"; 
				else
					whereCodes += "	ce.value like '"+words[i]+"%' or ";
					
			}
			whereCodes += " ) ";
				
		}

		
		String sql = 
				  
				  "			select coalesce(sum(fa.amtacctcr - fa.amtacctdr),0)	\n"
				+ "			from fact_acct fa, c_elementvalue ce 	\n"
				+ "			where fa.account_id = ce.c_elementvalue_id 	\n"
				+ "			and fa.c_bpartner_id =  " + bp
				;

		if(startDate != null && endDate != null){	
			sql +=	"		and fa.dateacct between '"+startDate+"'::date and '"+endDate+"'::date	\n"	;
		}else if(endDate == null){
			sql +=		"		and fa.dateacct <= '"+startDate+"'::date 	\n"	;
		}if(startDate == null){
			sql +=		"		and fa.dateacct <= '"+endDate+"'::date	\n"	;
		}
		
			sql += whereCodes;				

		

		try {
			pstmt = DB.prepareStatement(sql, null);

			rs = pstmt.executeQuery();
			if (rs.next()) {
				result = rs.getBigDecimal(1);
//				if(result.compareTo(Env.ZERO) != 0){
//						result = result.divide(new BigDecimal(1000));
//						result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
//				}
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "Report: FI_Form4", e);
			return new BigDecimal(0);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
		return result;
		
	}
	
	
	private List<Line> load5(MBCSFRInputFormCompany company) {
 		List<Line> list = new ArrayList<Line>();
		
		MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
		int C_Period_ID = period.getPeriodFrom();
		MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
		Long time;

		int C_Period_ID2 = period.getPeriodTo();
		MPeriod p2 = new MPeriod(Env.getCtx(), C_Period_ID2, null);
		
		Date enterD1 = p.getStartDate();
		
		Date d1 = p.getStartDate();
		Date d2 = p2.getEndDate();
		
		Calendar c = Calendar.getInstance();

		//ts3
		c.setTime(d1);
		c.add(Calendar.YEAR, -1);
		Date d3 = new Date(c.getTimeInMillis());
		
		//ts4
		c.setTime(p2.getEndDate());
		c.add(Calendar.YEAR, -1);
		Date d4 = new Date(c.getTimeInMillis());
		
		String codes = "";
		
		BigDecimal r010 = zero; BigDecimal r010p = zero;
		BigDecimal r011 = zero; BigDecimal r011p = zero;
		BigDecimal r012 = zero; BigDecimal r012p = zero;
		BigDecimal r013 = zero; BigDecimal r013p = zero;
		BigDecimal r014 = zero; BigDecimal r014p = zero;
		BigDecimal r015 = zero; BigDecimal r015p = zero;
		BigDecimal r016 = zero; BigDecimal r016p = zero;
		BigDecimal r020 = zero; BigDecimal r020p = zero;
		BigDecimal r021 = zero; BigDecimal r021p = zero;
		BigDecimal r022 = zero; BigDecimal r022p = zero;
		BigDecimal r023 = zero; BigDecimal r023p = zero;
		BigDecimal r024 = zero; BigDecimal r024p = zero;
		BigDecimal r025 = zero; BigDecimal r025p = zero;
		BigDecimal r026 = zero; BigDecimal r026p = zero;
		BigDecimal r027 = zero; BigDecimal r027p = zero;
		
		BigDecimal r030 = zero; BigDecimal r030p = zero;
		
		BigDecimal r040 = zero; BigDecimal r040p = zero;
		BigDecimal r041 = zero; BigDecimal r041p = zero;
		BigDecimal r042 = zero; BigDecimal r042p = zero;
		BigDecimal r043 = zero; BigDecimal r043p = zero;
		BigDecimal r044 = zero; BigDecimal r044p = zero;
		BigDecimal r045 = zero; BigDecimal r045p = zero;
		BigDecimal r046 = zero; BigDecimal r046p = zero;
		BigDecimal r047 = zero; BigDecimal r047p = zero;
		BigDecimal r048 = zero; BigDecimal r048p = zero;
		BigDecimal r049 = zero; BigDecimal r049p = zero;
		BigDecimal r050 = zero; BigDecimal r050p = zero;
		BigDecimal r051 = zero; BigDecimal r051p = zero;
		
		BigDecimal r060 = zero; BigDecimal r060p = zero;
		BigDecimal r061 = zero; BigDecimal r061p = zero;
		BigDecimal r062 = zero; BigDecimal r062p = zero;
		BigDecimal r063 = zero; BigDecimal r063p = zero;
		BigDecimal r064 = zero; BigDecimal r064p = zero;
		BigDecimal r065 = zero; BigDecimal r065p = zero;
		BigDecimal r066 = zero; BigDecimal r066p = zero;
		BigDecimal r067 = zero; BigDecimal r067p = zero;
		BigDecimal r068 = zero; BigDecimal r068p = zero;
		BigDecimal r069 = zero; BigDecimal r069p = zero;
		BigDecimal r070 = zero; BigDecimal r070p = zero;
		BigDecimal r071 = zero; BigDecimal r071p = zero;
		
		BigDecimal r080 = zero; BigDecimal r080p = zero;
		
		BigDecimal r090 = zero; BigDecimal r090p = zero;
		BigDecimal r091 = zero; BigDecimal r091p = zero;
		BigDecimal r092 = zero; BigDecimal r092p = zero;
		BigDecimal r093 = zero; BigDecimal r093p = zero;
		BigDecimal r094 = zero; BigDecimal r094p = zero;
		
		BigDecimal r100 = zero; BigDecimal r100p = zero;
		BigDecimal r101 = zero; BigDecimal r101p = zero;
		BigDecimal r102 = zero; BigDecimal r102p = zero;
		BigDecimal r103 = zero; BigDecimal r103p = zero;
		BigDecimal r104 = zero; BigDecimal r104p = zero;
		BigDecimal r105 = zero; BigDecimal r105p = zero;
		
		BigDecimal r110 = zero; BigDecimal r110p = zero;
//		BigDecimal r120 = zero; BigDecimal r120p = zero;
		BigDecimal r130 = zero; BigDecimal r130p = zero;
		BigDecimal r140 = zero; BigDecimal r140p = zero;
		BigDecimal r150 = zero; BigDecimal r150p = zero;
		
	
		codes = "011";
		r011 = getData5(codes, d1, d2);	r011p = getData5(codes, d3, d4);

		codes = "012";
		r012 = getData5(codes, d1, d2);	r012p = getData5(codes, d3, d4);
		
		codes = "013";
		r013 = getData5(codes, d1, d2);	r013p = getData5(codes, d3, d4);
		
		codes = "014";
		r014 = getData5(codes, d1, d2);	r014p = getData5(codes, d3, d4);

		codes = "015";
		r015 = getData5(codes, d1, d2);	r015p = getData5(codes, d3, d4);
		
		codes = "016";
		r016 = getData5(codes, d1, d2);	r016p = getData5(codes, d3, d4);
		
		r010 = r010.add(r011).add(r012).add(r013).add(r014).add(r015).add(r016); r010p = r010p.add(r011p).add(r012p).add(r013p).add(r014p).add(r015p).add(r016p);
				
		
		codes = "021";
		r021 = getData5(codes, d1, d2);	r021p = getData5(codes, d3, d4);

		codes = "022";
		r022 = getData5(codes, d1, d2);	r022p = getData5(codes, d3, d4);
		
		codes = "023";
		r023 = getData5(codes, d1, d2);	r023p = getData5(codes, d3, d4);

		codes = "024";
		r024 = getData5(codes, d1, d2);	r024p = getData5(codes, d3, d4);
		
		codes = "025";
		r025 = getData5(codes, d1, d2);	r025p = getData5(codes, d3, d4);
		
		
		double v061 = getData5("061", d1, d2).doubleValue();
		double v062 = getData5("062", d1, d2).doubleValue();
		double v026 = getData5("026", d1, d2).doubleValue();
		double vDiff = v061 / 112 * 100;
		  v026 += v061 - vDiff;
		  v061 = vDiff;
		  
		  vDiff = v062 / 112 * 100;
		  v026 += v062 - vDiff;
		  v062 = vDiff;

		  r026 = r026.add(new BigDecimal(v026));	r026 = r026;
		  r061 = r061.add(new BigDecimal(v061));	r061 = r061;
		  r062 = r062.add(new BigDecimal(v062));	r062 = r062;
		  
		double v061p = getData5("061", d3, d4).doubleValue();
		double v062p = getData5("062", d3, d4).doubleValue();
		double v026p = getData5("026", d3, d4).doubleValue();
			vDiff = v061p / 112 * 100;
		  v026p += v061p - vDiff;
		  v061p = vDiff;
		  
		  vDiff = v062p / 112 * 100;
		  v026p += v062p - vDiff;
		  v062p = vDiff;

		  r026p = r026p.add(new BigDecimal(v026p));	r026p = r026p;
		  r061p = r061p.add(new BigDecimal(v061p));	r061p = r061p;
		  r062p = r062p.add(new BigDecimal(v062p));	r062p = r062p;
		  
		codes = "027";
		r027 = getData5(codes, d1, d2);	r027p = getData5(codes, d3, d4);		  
		
		r020 = r020.add(r021).add(r022).add(r023).add(r024).add(r025).add(r026).add(r027);		r020 = r020;
		r020p = r020p.add(r021p).add(r022p).add(r023p).add(r024p).add(r025p).add(r026p).add(r027p); r020p = r020p;		

		r030 = r010.subtract(r020);	r030p = r010p.subtract(r020p); 
		r030 = r030; r030p = r030p;

		codes = "041";
		r041 = getData5(codes, d1, d2);	r041p = getData5(codes, d3, d4);		  
		codes = "042";
		r042 = getData5(codes, d1, d2);	r042p = getData5(codes, d3, d4);		  
		codes = "043";
		r043 = getData5(codes, d1, d2);	r043p = getData5(codes, d3, d4);		  
		codes = "044";
		r044 = getData5(codes, d1, d2);	r044p = getData5(codes, d3, d4);		  
		codes = "045";
		r045 = getData5(codes, d1, d2);	r045p = getData5(codes, d3, d4);		  
		codes = "046";
		r046 = getData5(codes, d1, d2);	r046p = getData5(codes, d3, d4);		  
		codes = "047";
		r047 = getData5(codes, d1, d2);	r047p = getData5(codes, d3, d4);		  
		codes = "048";
		r048 = getData5(codes, d1, d2);	r048p = getData5(codes, d3, d4);		  
		codes = "049";
		r049 = getData5(codes, d1, d2);	r049p = getData5(codes, d3, d4);		  
		codes = "050";
		r050 = getData5(codes, d1, d2);	r050p = getData5(codes, d3, d4);		  
		codes = "051";
		r051 = getData5(codes, d1, d2);	r051p = getData5(codes, d3, d4);		  

		r040 = r040.add(r041).add(r042).add(r043).add(r044).add(r045).add(r046).add(r047).add(r048).add(r049).add(r050).add(r051);
		r040 = r040;
		r040p = r040p.add(r041p).add(r042p).add(r043p).add(r044p).add(r045p).add(r046p).add(r047p).add(r048p).add(r049p).add(r050p).add(r051p);
		r040p = r040p;
		
		codes = "063";
		r063 = getData5(codes, d1, d2);	r063p = getData5(codes, d3, d4);		  
		codes = "064";
		r064 = getData5(codes, d1, d2);	r064p = getData5(codes, d3, d4);		  
		codes = "065";
		r065 = getData5(codes, d1, d2);	r065p = getData5(codes, d3, d4);		  
		codes = "066";
		r066 = getData5(codes, d1, d2);	r066p = getData5(codes, d3, d4);		  
		codes = "067";
		r067 = getData5(codes, d1, d2);	r067p = getData5(codes, d3, d4);		  
		codes = "068";
		r068 = getData5(codes, d1, d2);	r068p = getData5(codes, d3, d4);		  
		codes = "069";
		r069 = getData5(codes, d1, d2);	r069p = getData5(codes, d3, d4);		  
		codes = "070";
		r070 = getData5(codes, d1, d2);	r070p = getData5(codes, d3, d4);		  
		codes = "071";
		r071 = getData5(codes, d1, d2);	r071p = getData5(codes, d3, d4);
		
		r060 = r060.add(r061).add(r062).add(r063).add(r064).add(r065).add(r066).add(r067).add(r068).add(r069).add(r070).add(r071);
		r060 = r060;
		r060p = r060p.add(r061p).add(r062p).add(r063p).add(r064p).add(r065p).add(r066p).add(r067p).add(r068p).add(r069p).add(r070p).add(r071p);
		r060p = r060p;
		
		r080 = r040.subtract(r060);		r080p = r040p.subtract(r060p);
		r080 = r080;
		r080p = r080p;
		
		codes = "091";
		r091 = getData5(codes, d1, d2);	r091p = getData5(codes, d3, d4);		  
		codes = "092";
		r092 = getData5(codes, d1, d2);	r092p = getData5(codes, d3, d4);		  
		codes = "093";
		r093 = getData5(codes, d1, d2);	r093p = getData5(codes, d3, d4);		  
		codes = "094";
		r094 = getData5(codes, d1, d2);	r094p = getData5(codes, d3, d4);
		
		r090 = r090.add(r091).add(r092).add(r093).add(r094);	r090p = r090p.add(r091p).add(r092p).add(r093p).add(r094p);
		r090 = r090;
		r090p = r090p;
		
		codes = "101";
		r101 = getData5(codes, d1, d2);	r101p = getData5(codes, d3, d4);		  
		codes = "102";
		r102 = getData5(codes, d1, d2);	r102p = getData5(codes, d3, d4);		  
		codes = "103";
		r103 = getData5(codes, d1, d2);	r103p = getData5(codes, d3, d4);		  
		codes = "104";
		r104 = getData5(codes, d1, d2);	r104p = getData5(codes, d3, d4);		  
		codes = "105";
		r105 = getData5(codes, d1, d2);	r105p = getData5(codes, d3, d4);
		
		r100 = r100.add(r101).add(r102).add(r103).add(r104).add(r105);	r100p = r100p.add(r101p).add(r102p).add(r103p).add(r104p).add(r105p);
		r100 = r100;
		r100p = r100p;
		
		r110 = r090.subtract(r100);		r110p = r090p.subtract(r100p);
		r110 = r110;
		r110p = r110p;
		
		Timestamp enterD1ts = new Timestamp(enterD1.getTime());
		
		r140 = getDatabyAcct(d1, d2, "104%");	r140p = getDatabyAcct(d3, d4, "104%");

		r130 = r130.add(r030).add(r080).add(r110);	r130p = r130p.add(r030p).add(r080p).add(r110p);
		r130 = r130;
		r130p = r130p;
		
		r150 = r150.add(r130).add(r140);	r150p = r150p.add(r130p).add(r140p);
		r150 = r150;
		r150p = r150p;
		Line line;
		
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	010	*/	line = new Line();		line.setValue1(getDiv1000(r010).toString());	line.setValue2(getDiv1000(r010p).toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	011	*/	line = new Line();		line.setValue1(getDiv1000(r011).toString());	line.setValue2(getDiv1000(r011p).toString());	list.add(line);
		/*	012	*/	line = new Line();		line.setValue1(getDiv1000(r012).toString());	line.setValue2(getDiv1000(r012p).toString());	list.add(line);
		/*	013	*/	line = new Line();		line.setValue1(getDiv1000(r013).toString());	line.setValue2(getDiv1000(r013p).toString());	list.add(line);
		/*	014	*/	line = new Line();		line.setValue1(getDiv1000(r014).toString());	line.setValue2(getDiv1000(r014p).toString());	list.add(line);
		/*	015	*/	line = new Line();		line.setValue1(getDiv1000(r015).toString());	line.setValue2(getDiv1000(r015p).toString());	list.add(line);
		/*	016	*/	line = new Line();		line.setValue1(getDiv1000(r016).toString());	line.setValue2(getDiv1000(r016p).toString());	list.add(line);
		
		/*	020	*/	line = new Line();		line.setValue1(getDiv1000(r020).toString());	line.setValue2(getDiv1000(r020p).toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);		
		/*	021	*/	line = new Line();		line.setValue1(getDiv1000(r021).toString());	line.setValue2(getDiv1000(r021p).toString());	list.add(line);
		/*	022	*/	line = new Line();		line.setValue1(getDiv1000(r022).toString());	line.setValue2(getDiv1000(r022p).toString());	list.add(line);
		/*	023	*/	line = new Line();		line.setValue1(getDiv1000(r023).toString());	line.setValue2(getDiv1000(r023p).toString());	list.add(line);
		/*	024	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	025	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	026	*/	line = new Line();		line.setValue1(getDiv1000(r026).toString());	line.setValue2(getDiv1000(r026p).toString());	list.add(line);
		/*	027	*/	line = new Line();		line.setValue1(getDiv1000(r027).toString());	line.setValue2(getDiv1000(r027p).toString());	list.add(line);
		/*	030	*/	line = new Line();		line.setValue1(getDiv1000(r030).toString());	line.setValue2(getDiv1000(r030p).toString());	list.add(line);
		
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		
		/*	040	*/	line = new Line();		line.setValue1(getDiv1000(r040).toString());	line.setValue2(getDiv1000(r040p).toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	041	*/	line = new Line();		line.setValue1(getDiv1000(r041).toString());	line.setValue2(getDiv1000(r041p).toString());	list.add(line);
		/*	042	*/	line = new Line();		line.setValue1(getDiv1000(r042).toString());	line.setValue2(getDiv1000(r042p).toString());	list.add(line);
		/*	043	*/	line = new Line();		line.setValue1(getDiv1000(r043).toString());	line.setValue2(getDiv1000(r043p).toString());	list.add(line);
		/*	044	*/	line = new Line();		line.setValue1(getDiv1000(r044).toString());	line.setValue2(getDiv1000(r044p).toString());	list.add(line);
		/*	045	*/	line = new Line();		line.setValue1(getDiv1000(r045).toString());	line.setValue2(getDiv1000(r045p).toString());	list.add(line);
		/*	046	*/	line = new Line();		line.setValue1(getDiv1000(r046).toString());	line.setValue2(getDiv1000(r046p).toString());	list.add(line);
		/*	047	*/	line = new Line();		line.setValue1(getDiv1000(r047).toString());	line.setValue2(getDiv1000(r047p).toString());	list.add(line);		
		/*	048	*/	line = new Line();		line.setValue1(getDiv1000(r048).toString());	line.setValue2(getDiv1000(r048p).toString());	list.add(line);
		/*	049	*/	line = new Line();		line.setValue1(getDiv1000(r049).toString());	line.setValue2(getDiv1000(r049p).toString());	list.add(line);
		/*	050	*/	line = new Line();		line.setValue1(getDiv1000(r050).toString());	line.setValue2(getDiv1000(r050p).toString());	list.add(line);
		/*	051	*/	line = new Line();		line.setValue1(getDiv1000(r051).toString());	line.setValue2(getDiv1000(r051p).toString());	list.add(line);
		
		/*	60	*/	line = new Line();		line.setValue1(getDiv1000(r060).toString());	line.setValue2(getDiv1000(r060p).toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);		
		/*	61	*/	line = new Line();		line.setValue1(getDiv1000(r061).toString());	line.setValue2(getDiv1000(r061p).toString());	list.add(line);
		/*	62	*/	line = new Line();		line.setValue1(getDiv1000(r062).toString());	line.setValue2(getDiv1000(r062p).toString());	list.add(line);
		/*	63	*/	line = new Line();		line.setValue1(getDiv1000(r063).toString());	line.setValue2(getDiv1000(r063p).toString());	list.add(line);
		/*	64	*/	line = new Line();		line.setValue1(getDiv1000(r064).toString());	line.setValue2(getDiv1000(r064p).toString());	list.add(line);
		/*	65	*/	line = new Line();		line.setValue1(getDiv1000(r065).toString());	line.setValue2(getDiv1000(r065p).toString());	list.add(line);
		/*	66	*/	line = new Line();		line.setValue1(getDiv1000(r066).toString());	line.setValue2(getDiv1000(r066p).toString());	list.add(line);
		/*	67	*/	line = new Line();		line.setValue1(getDiv1000(r067).toString());	line.setValue2(getDiv1000(r067p).toString());	list.add(line);
		/*	68	*/	line = new Line();		line.setValue1(getDiv1000(r068).toString());	line.setValue2(getDiv1000(r068p).toString());	list.add(line);
		/*	69	*/	line = new Line();		line.setValue1(getDiv1000(r069).toString());	line.setValue2(getDiv1000(r069p).toString());	list.add(line);
		/*	70	*/	line = new Line();		line.setValue1(getDiv1000(r070).toString());	line.setValue2(getDiv1000(r070p).toString());	list.add(line);
		/*	71	*/	line = new Line();		line.setValue1(getDiv1000(r071).toString());	line.setValue2(getDiv1000(r071p).toString());	list.add(line);

		/*	80	*/	line = new Line();		line.setValue1(getDiv1000(r080).toString());	line.setValue2(getDiv1000(r080p).toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);		
		/*	90	*/	line = new Line();		line.setValue1(getDiv1000(r090).toString());	line.setValue2(getDiv1000(r090p).toString());	list.add(line);		
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	91	*/	line = new Line();		line.setValue1(getDiv1000(r091).toString());	line.setValue2(getDiv1000(r091p).toString());	list.add(line);
		/*	92	*/	line = new Line();		line.setValue1(getDiv1000(r092).toString());	line.setValue2(getDiv1000(r092p).toString());	list.add(line);
		/*	93	*/	line = new Line();		line.setValue1(getDiv1000(r093).toString());	line.setValue2(getDiv1000(r093p).toString());	list.add(line);
		/*	94	*/	line = new Line();		line.setValue1(getDiv1000(r094).toString());	line.setValue2(getDiv1000(r094p).toString());	list.add(line);
		/*	100	*/	line = new Line();		line.setValue1(getDiv1000(r100).toString());	line.setValue2(getDiv1000(r100p).toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	101	*/	line = new Line();		line.setValue1(getDiv1000(r101).toString());	line.setValue2(getDiv1000(r101p).toString());	list.add(line);		
		/*	102	*/	line = new Line();		line.setValue1(getDiv1000(r102).toString());	line.setValue2(getDiv1000(r102p).toString());	list.add(line);
		/*	103	*/	line = new Line();		line.setValue1(getDiv1000(r103).toString());	line.setValue2(getDiv1000(r103p).toString());	list.add(line);
		/*	104	*/	line = new Line();		line.setValue1(getDiv1000(r104).toString());	line.setValue2(getDiv1000(r104p).toString());	list.add(line);
		/*	105	*/	line = new Line();		line.setValue1(getDiv1000(r105).toString());	line.setValue2(getDiv1000(r105p).toString());	list.add(line);
		/*	110	*/	line = new Line();		line.setValue1(getDiv1000(r110).toString());	line.setValue2(getDiv1000(r110p).toString());	list.add(line);
		/*	120	*/	line = new Line();		line.setValue1(zero.toString());	line.setValue2(zero.toString());	list.add(line);
		/*	130	*/	line = new Line();		line.setValue1(getDiv1000(r130).toString());	line.setValue2(getDiv1000(r130p).toString());	list.add(line);
		/*	140	*/	line = new Line();		line.setValue1(getDiv1000(r140).toString());	line.setValue2(getDiv1000(r140p).toString());	list.add(line);
		/*	150	*/	line = new Line();		line.setValue1(getDiv1000(r150).toString());	line.setValue2(getDiv1000(r150p).toString());	list.add(line);
		
		return list;

	}

	private BigDecimal getDatabyAcct(Date d, Date d2, String acct) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		BigDecimal result = zero;

		StringBuilder sql = getSQlByAcct(d,d2, acct);

		try {
			pstmt = DB.prepareStatement(sql.toString(), null);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				result = rs.getBigDecimal(1);
			}

			if(result==null) result = zero;
//			if(result.compareTo(zero) != 0 )
//				result = result.divide(new BigDecimal(1000));
//			
//			result = result.setScale(0, BigDecimal.ROUND_HALF_UP);

		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "Report: FI_Form3Direct", e);
//			getSQlresult = e.getMessage();
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
		return result;
	}
	
	private StringBuilder getSQlByAcct(Date PstartDate,Date PendDate, String acct) {
		StringBuilder sql = new StringBuilder();
		sql.append(
				  " select coalesce(sum(fa.amtacctdr - fa.amtacctcr),0) from fact_acct fa, c_elementvalue ce " 
				+ " where fa.account_id=ce.c_elementvalue_id	"
				+ " and PostingType='A' "		  
				+ "and fa.dateacct < '"+PstartDate+"' and ce.value like '"+acct+"'"
				);				
					
		return sql;
	}
	
	private BigDecimal getData5(String code, Date dateFrom, Date dateTo){
		BigDecimal result = zero;
		StringBuffer sql = new StringBuffer();
		
		sql.append(
				" select(	\n"
				+ " 						(SELECT coalesce(abs(sum(l.stmtAmt)),0)	\n"
				+ " 							FROM adempiere.c_bankstatement h	\n"
				+ " 							inner join adempiere.c_bankstatementline l on l.c_bankstatement_id = h.c_bankstatement_id	\n"
				+ " 							inner join fi_form3 f3 on f3.fi_form3_id = l.fi_form3_id	\n"
				+ " 							where l.dateacct between '" + dateFrom + "' and '"+ dateTo +"' 	\n"
				+ " 							and f3.fi_code = '" + code + "'	\n" 
//				+ " 							and f3.fi_form3_id not in (select fi_form3_id from adempiere.fi_form3exclude where isactive = 'Y' )  \n"
				+ "								and h.posted = 'Y'	\n"			
				+ " 						)	\n"
				+ " 						+ 	\n"
				+ " 						(select	\n" 
				+ " 							coalesce(abs(sum(cp.payamt)),0)	\n"
				+ " 							from c_payment cp 		\n"
				+ " 							inner join fi_form3 f3 on f3.fi_form3_id = cp.fi_form3_id	\n"	 
				+ " 							where dateacct between '" + dateFrom + "' and '"+ dateTo +"' 		\n"
				+ " 							and f3.fi_code = '" + code + "'	\n"
//				+ " 							and f3.fi_form3_id not in (select fi_form3_id from adempiere.fi_form3exclude where isactive = 'Y' )  \n"
				+ "								and cp.posted = 'Y'	\n"				
				+ " 						)	\n"
				+ " 						+	\n"
				+ " 						(	\n"
				+ " 						select coalesce(abs(sum(l.amtsourcedr)),0)	\n"
				+ " 							from adempiere.gl_journal h	\n"
				+ " 							inner join adempiere.gl_journalline l on l.gl_journal_id = h.gl_journal_id	\n"
				+ " 							inner join fi_form3 f3 on f3.fi_form3_id = l.fi_form3_id 	\n"
				+ " 							where h.dateacct between '" + dateFrom + "' and '"+ dateTo +"'	\n"
				+ " 							and f3.fi_code = '" + code + "'	\n"
//				+ " 							and f3.fi_form3_id not in (select fi_form3_id from adempiere.fi_form3exclude where isactive = 'Y' )  \n"
				+ "								and h.posted = 'Y'	\n"				
				+ " 						)	\n"
				+ " 					) amt	\n"
				);		
		
////		String[] words = codes.split(",");  
//		
////		String whereCodes = "";
////		if(words.length > 0){
////			whereCodes = " and ( ";
////			for(int i=0; i < words.length; i++){
////				if(i == (words.length-1))
////					whereCodes += "	ce.value like '"+words[i]+"%'	"; 
////				else
////					whereCodes += "	ce.value like '"+words[i]+"%' or ";
////					
////			}
////			whereCodes += " ) ";
////				
////		}
//		String whereCodes = "";
//		if(words.length > 0){
//			whereCodes = " and c_charge_id in ( ";
//			for(int i=0; i < words.length; i++){
//				if(i == (words.length-1))
//					whereCodes += words[i]; 
//				else
//					whereCodes += words[i]+",";
//					
//			}
//			whereCodes += " ) \n";
//		}
//		
//		String sql = 
//				  
//				  "			select coalesce(sum(fa.amtacctcr),0)	\n"
//				+ "			from fact_acct fa \n"//, c_elementvalue ce 	\n"
////				+ "			where fa.account_id = ce.c_elementvalue_id 	\n"
//				;
//
//		if(startDate != null && endDate != null){	
//			sql +=	"		where fa.dateacct between '"+startDate+"'::date and '"+endDate+"'::date	\n"	;
//		}else if(endDate == null){
//			sql +=		"		where fa.dateacct <= '"+startDate+"'::date 	\n"	;
//		}if(startDate == null){
//			sql +=		"		where fa.dateacct <= '"+endDate+"'::date	\n"	;
//		}
//		
//			sql += whereCodes;			
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			pstmt = DB.prepareStatement(sql.toString(), null);
//			pstmt = DB.prepareStatement(s, null);
//			System.out.println(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
					result = rs.getBigDecimal(1);
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
		
//		if(result.compareTo(zero) != 0 )
//			result = result.divide(new BigDecimal(1000));
//		
//		result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
		
		return result; 
	}
	
	private List<Line> loadICS3(MBCSFRInputFormCompany company, MBCSFRInputForm form) {
		List<Line> list = new ArrayList<Line>();
		
		MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
		int C_Period_ID = period.getPeriodFrom();
		MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
		Date d1 = p.getStartDate();

		int C_Period_ID2 = period.getPeriodTo();
		MPeriod p2 = new MPeriod(Env.getCtx(), C_Period_ID2, null);
		Date d2 = p.getEndDate();
		
		List<MBCSFRInputFormLine> lines = getMBCSFRInputFormLines(form);
		List<MBCSFRInputFormLine> l = new ArrayList<MBCSFRInputFormLine>();
		if(lines.size() == 0)
			return list;
		
		l.add(lines.get(0));
		
		List<MBCSFRInputFormColumn> columns = getMBCSFRInputFormColumn(l); 


		String codes = "";
		
 
		iline r011 = getDataICS3("r011", columns, "011", d1, d2,1); 
		iline r012 = getDataICS3("r012", columns, "012", d1, d2,1); 
		iline r013 = getDataICS3("r013", columns, "013", d1, d2,1); 
		iline r014 = getDataICS3("r014", columns, "014", d1, d2,1); 
		iline r015 = getDataICS3("r015", columns, "015", d1, d2,1); 
		iline r016 = getDataICS3("r016", columns, "016", d1, d2,1);
		
		iline r010 = new iline(); r010.setCode("r010");
		r010.setF1(r010.getF1().add(r011.getF1()).add(r012.getF1()).add(r013.getF2()).add(r014.getF1()).add(r015.getF1()).add(r016.getF1())); 
		r010.setF2(r010.getF2().add(r011.getF2()).add(r012.getF2()).add(r013.getF2()).add(r014.getF2()).add(r015.getF2()).add(r016.getF2()));
		r010.setF3(r010.getF3().add(r011.getF3()).add(r012.getF3()).add(r013.getF2()).add(r014.getF3()).add(r015.getF3()).add(r016.getF3()));
		r010.setF4(r010.getF4().add(r011.getF4()).add(r012.getF4()).add(r013.getF2()).add(r014.getF4()).add(r015.getF4()).add(r016.getF4()));
		
		r010.setF5(r010.getF5().add(r011.getF5()).add(r012.getF5()).add(r013.getF2()).add(r014.getF5()).add(r015.getF5()).add(r016.getF5()));
		r010.setF6(r010.getF6().add(r011.getF6()).add(r012.getF6()).add(r013.getF2()).add(r014.getF6()).add(r015.getF6()).add(r016.getF6()));
		r010.setF7(r010.getF7().add(r011.getF7()).add(r012.getF7()).add(r013.getF2()).add(r014.getF7()).add(r015.getF7()).add(r016.getF7()));
		r010.setF8(r010.getF8().add(r011.getF8()).add(r012.getF8()).add(r013.getF2()).add(r014.getF8()).add(r015.getF8()).add(r016.getF8()));

		
		
		iline r020 = new iline(); r020.setCode("r020"); 
		iline r021 = getDataICS3("r021", columns, "021", d1, d2,1); 
		iline r022 = getDataICS3("r022", columns, "022", d1, d2,1); 
		iline r023 = getDataICS3("r023", columns, "023", d1, d2,1); 
		iline r024 = getDataICS3("r024", columns, "024", d1, d2,1); 
		iline r025 = getDataICS3("r025", columns, "025", d1, d2,1); 
		iline r026 = getDataICS3("r026", columns, "026", d1, d2,1); 
		 
		
//		double v061 = getData5("061", d1, d2).doubleValue();
//		double v062 = getData5("062", d1, d2).doubleValue();
//		double v026 = getData5("026", d1, d2).doubleValue();
//		double vDiff = v061 / 112 * 100;
//		  v026 += v061 - vDiff;
//		  v061 = vDiff;
//		  
//		  vDiff = v062 / 112 * 100;
//		  v026 += v062 - vDiff;
//		  v062 = vDiff;
//
//		  r026 = r026.add(new BigDecimal(v026));	
//		  r061 = r061.add(new BigDecimal(v061));	
//		  r062 = r062.add(new BigDecimal(v062));	

		iline v061 = getDataICS3("061", columns, "061", d1, d2,1);
		iline v062 = getDataICS3("062", columns, "062", d1, d2,1);
		iline v026 = getDataICS3("026", columns, "026", d1, d2,1);
		iline vDiff = new iline();
		
			v026.setF1(v061.getF1().subtract(vDiff.getF1()));
			v026.setF2(v061.getF2().subtract(vDiff.getF2()));
			v026.setF3(v061.getF3().subtract(vDiff.getF3()));
			v026.setF4(v061.getF4().subtract(vDiff.getF4()));
			
			v026.setF5(v061.getF5().subtract(vDiff.getF5()));
			v026.setF6(v061.getF6().subtract(vDiff.getF6()));
			v026.setF7(v061.getF7().subtract(vDiff.getF7()));
			v026.setF8(v061.getF8().subtract(vDiff.getF8()));
			
			v061 = vDiff;
			
			vDiff.setF1(v062.getF1().divide(new BigDecimal(112)).multiply(new BigDecimal(100)));
			vDiff.setF2(v062.getF2().divide(new BigDecimal(112)).multiply(new BigDecimal(100)));
			vDiff.setF3(v062.getF3().divide(new BigDecimal(112)).multiply(new BigDecimal(100)));
			vDiff.setF4(v062.getF4().divide(new BigDecimal(112)).multiply(new BigDecimal(100)));
			
			vDiff.setF5(v062.getF5().divide(new BigDecimal(112)).multiply(new BigDecimal(100)));
			vDiff.setF6(v062.getF6().divide(new BigDecimal(112)).multiply(new BigDecimal(100)));
			vDiff.setF7(v062.getF7().divide(new BigDecimal(112)).multiply(new BigDecimal(100)));
			vDiff.setF8(v062.getF8().divide(new BigDecimal(112)).multiply(new BigDecimal(100)));
			
			
			v026.setF1(v026.getF1().add(v062.getF1()).subtract(vDiff.getF1()));
			v026.setF2(v026.getF2().add(v062.getF2()).subtract(vDiff.getF2()));
			v026.setF3(v026.getF3().add(v062.getF3()).subtract(vDiff.getF3()));
			v026.setF4(v026.getF4().add(v062.getF4()).subtract(vDiff.getF4()));
			
			v026.setF5(v026.getF5().add(v062.getF5()).subtract(vDiff.getF5()));
			v026.setF6(v026.getF6().add(v062.getF6()).subtract(vDiff.getF6()));
			v026.setF7(v026.getF7().add(v062.getF7()).subtract(vDiff.getF7()));
			v026.setF8(v026.getF8().add(v062.getF8()).subtract(vDiff.getF8()));
			
			v062 = vDiff;
			
		
		iline r027 = getDataICS3("r027", columns, "027", d1, d2,1);		  
		
//		r020 = r020.add(r021).add(r022).add(r023).add(r024).add(r025).add(r026).add(r027);		r020 = r020;
				
		r020.setF1(r020.getF1().add(r021.getF1()).add(r022.getF1()).add(r023.getF2()).add(r024.getF1()).add(r025.getF1()).add(r026.getF1()).add(r027.getF1())); 
		r020.setF2(r020.getF2().add(r021.getF2()).add(r022.getF2()).add(r023.getF2()).add(r024.getF2()).add(r025.getF2()).add(r026.getF2()).add(r027.getF2()));
		r020.setF3(r020.getF3().add(r021.getF3()).add(r022.getF3()).add(r023.getF2()).add(r024.getF3()).add(r025.getF3()).add(r026.getF3()).add(r027.getF3()));
		r020.setF4(r020.getF4().add(r021.getF4()).add(r022.getF4()).add(r023.getF2()).add(r024.getF4()).add(r025.getF4()).add(r026.getF4()).add(r027.getF4()));
		
		r020.setF5(r020.getF5().add(r021.getF5()).add(r022.getF5()).add(r023.getF2()).add(r024.getF5()).add(r025.getF5()).add(r026.getF5()).add(r027.getF5()));
		r020.setF6(r020.getF6().add(r021.getF6()).add(r022.getF6()).add(r023.getF2()).add(r024.getF6()).add(r025.getF6()).add(r026.getF6()).add(r027.getF6()));
		r020.setF7(r020.getF7().add(r021.getF7()).add(r022.getF7()).add(r023.getF2()).add(r024.getF7()).add(r025.getF7()).add(r026.getF7()).add(r027.getF7()));
		r020.setF8(r020.getF8().add(r021.getF8()).add(r022.getF8()).add(r023.getF2()).add(r024.getF8()).add(r025.getF8()).add(r026.getF8()).add(r027.getF8()));

//		r030 = r010.subtract(r020);			
		iline r030 = new iline(); r030.setCode("r030"); 
		r030.setF1(r010.getF1().subtract(r021.getF1())); 
		r030.setF2(r010.getF2().subtract(r021.getF2()));
		r030.setF3(r010.getF3().subtract(r021.getF3()));
		r030.setF4(r010.getF4().subtract(r021.getF4()));
		
		r030.setF5(r010.getF5().subtract(r021.getF5()));
		r030.setF6(r010.getF6().subtract(r021.getF6()));
		r030.setF7(r010.getF7().subtract(r021.getF7()));
		r030.setF8(r010.getF8().subtract(r021.getF8()));
		
		
		iline r040 = getDataICS3("r040", columns, "011", d1, d2,1); 
		iline r041 = getDataICS3("r041", columns, "041", d1, d2,1); 
		iline r042 = getDataICS3("r042", columns, "042", d1, d2,1); 
		iline r043 = getDataICS3("r043", columns, "043", d1, d2,1); 
		iline r044 = getDataICS3("r044", columns, "044", d1, d2,1); 
		iline r045 = getDataICS3("r045", columns, "045", d1, d2,1); 
		iline r046 = getDataICS3("r046", columns, "046", d1, d2,1); 
		iline r047 = getDataICS3("r047", columns, "047", d1, d2,1); 
		iline r048 = getDataICS3("r048", columns, "048", d1, d2,1); 
		iline r049 = getDataICS3("r049", columns, "049", d1, d2,1); 
		iline r050 = getDataICS3("r050", columns, "050", d1, d2,1); 
		iline r051 = getDataICS3("r051", columns, "051", d1, d2,1); 
		
//		r040 = r040.add(r041).add(r042).add(r043).add(r044).add(r045).add(r046).add(r047).add(r048).add(r049).add(r050).add(r051);		
		r040.setF1(r040.getF1().add(r041.getF1()).add(r042.getF1()).add(r043.getF2()).add(r044.getF1()).add(r045.getF1()).add(r046.getF1()).add(r047.getF1()).add(r047.getF1()).add(r048.getF1()).add(r049.getF1()).add(r050.getF1()).add(r051.getF1())); 
		r040.setF2(r040.getF2().add(r041.getF2()).add(r042.getF2()).add(r043.getF2()).add(r044.getF2()).add(r045.getF2()).add(r046.getF2()).add(r047.getF2()).add(r047.getF1()).add(r048.getF1()).add(r049.getF1()).add(r050.getF1()).add(r051.getF1()));
		r040.setF3(r040.getF3().add(r041.getF3()).add(r042.getF3()).add(r043.getF2()).add(r044.getF3()).add(r045.getF3()).add(r046.getF3()).add(r047.getF3()).add(r047.getF1()).add(r048.getF1()).add(r049.getF1()).add(r050.getF1()).add(r051.getF1()));
		r040.setF4(r040.getF4().add(r041.getF4()).add(r042.getF4()).add(r043.getF2()).add(r044.getF4()).add(r045.getF4()).add(r046.getF4()).add(r047.getF4()).add(r047.getF1()).add(r048.getF1()).add(r049.getF1()).add(r050.getF1()).add(r051.getF1()));
		
		r040.setF5(r040.getF5().add(r041.getF5()).add(r042.getF5()).add(r043.getF2()).add(r044.getF5()).add(r045.getF5()).add(r046.getF5()).add(r047.getF5()).add(r047.getF1()).add(r048.getF1()).add(r049.getF1()).add(r050.getF1()).add(r051.getF1()));
		r040.setF6(r040.getF6().add(r041.getF6()).add(r042.getF6()).add(r043.getF2()).add(r044.getF6()).add(r045.getF6()).add(r046.getF6()).add(r047.getF6()).add(r047.getF1()).add(r048.getF1()).add(r049.getF1()).add(r050.getF1()).add(r051.getF1()));
		r040.setF7(r040.getF7().add(r041.getF7()).add(r042.getF7()).add(r043.getF2()).add(r044.getF7()).add(r045.getF7()).add(r046.getF7()).add(r047.getF7()).add(r047.getF1()).add(r048.getF1()).add(r049.getF1()).add(r050.getF1()).add(r051.getF1()));
		r040.setF8(r040.getF8().add(r041.getF8()).add(r042.getF8()).add(r043.getF2()).add(r044.getF8()).add(r045.getF8()).add(r046.getF8()).add(r047.getF8()).add(r047.getF1()).add(r048.getF1()).add(r049.getF1()).add(r050.getF1()).add(r051.getF1()));
		
		
		
		iline r060 = new iline(); r060.setCode("r061"); 
		iline r061 = getDataICS3("r061", columns, "061", d1, d2,1); 
		iline r062 = getDataICS3("r062", columns, "062", d1, d2,1); 
		iline r063 = getDataICS3("r063", columns, "063", d1, d2,1); 
		iline r064 = getDataICS3("r064", columns, "064", d1, d2,1); 
		iline r065 = getDataICS3("r065", columns, "065", d1, d2,1); 
		iline r066 = getDataICS3("r066", columns, "066", d1, d2,1); 
		iline r067 = getDataICS3("r067", columns, "067", d1, d2,1); 
		iline r068 = getDataICS3("r068", columns, "068", d1, d2,1); 
		iline r069 = getDataICS3("r069", columns, "069", d1, d2,1); 
		iline r070 = getDataICS3("r070", columns, "071", d1, d2,1); 
		iline r071 = getDataICS3("r071", columns, "071", d1, d2,1); 

//		r060 = r060.add(r061).add(r062).add(r063).add(r064).add(r065).add(r066).add(r067).add(r068).add(r069).add(r070).add(r071);
		r060.setF1(r060.getF1().add(r061.getF1()).add(r062.getF1()).add(r063.getF2()).add(r064.getF1()).add(r065.getF1()).add(r066.getF1()).add(r067.getF1()).add(r067.getF1()).add(r068.getF1()).add(r069.getF1()).add(r070.getF1()).add(r071.getF1())); 
		r060.setF2(r060.getF2().add(r061.getF2()).add(r062.getF2()).add(r063.getF2()).add(r064.getF2()).add(r065.getF2()).add(r066.getF2()).add(r067.getF2()).add(r067.getF1()).add(r068.getF1()).add(r069.getF1()).add(r070.getF1()).add(r071.getF1()));
		r060.setF3(r060.getF3().add(r061.getF3()).add(r062.getF3()).add(r063.getF2()).add(r064.getF3()).add(r065.getF3()).add(r066.getF3()).add(r067.getF3()).add(r067.getF1()).add(r068.getF1()).add(r069.getF1()).add(r070.getF1()).add(r071.getF1()));
		r060.setF4(r060.getF4().add(r061.getF4()).add(r062.getF4()).add(r063.getF2()).add(r064.getF4()).add(r065.getF4()).add(r066.getF4()).add(r067.getF4()).add(r067.getF1()).add(r068.getF1()).add(r069.getF1()).add(r070.getF1()).add(r071.getF1()));
		
		r060.setF5(r060.getF5().add(r061.getF5()).add(r062.getF5()).add(r063.getF2()).add(r064.getF5()).add(r065.getF5()).add(r066.getF5()).add(r067.getF5()).add(r067.getF1()).add(r068.getF1()).add(r069.getF1()).add(r070.getF1()).add(r071.getF1()));
		r060.setF6(r060.getF6().add(r061.getF6()).add(r062.getF6()).add(r063.getF2()).add(r064.getF6()).add(r065.getF6()).add(r066.getF6()).add(r067.getF6()).add(r067.getF1()).add(r068.getF1()).add(r069.getF1()).add(r070.getF1()).add(r071.getF1()));
		r060.setF7(r060.getF7().add(r061.getF7()).add(r062.getF7()).add(r063.getF2()).add(r064.getF7()).add(r065.getF7()).add(r066.getF7()).add(r067.getF7()).add(r067.getF1()).add(r068.getF1()).add(r069.getF1()).add(r070.getF1()).add(r071.getF1()));
		r060.setF8(r060.getF8().add(r061.getF8()).add(r062.getF8()).add(r063.getF2()).add(r064.getF8()).add(r065.getF8()).add(r066.getF8()).add(r067.getF8()).add(r067.getF1()).add(r068.getF1()).add(r069.getF1()).add(r070.getF1()).add(r071.getF1()));
		
//		r080 = r040.subtract(r060);				
		iline r080 = new iline(); r080.setCode("r080");
		r080.setF1(r040.getF1().subtract(r060.getF1())); 
		r080.setF2(r040.getF2().subtract(r060.getF2()));
		r080.setF3(r040.getF3().subtract(r060.getF3()));
		r080.setF4(r040.getF4().subtract(r060.getF4()));
		
		r080.setF5(r040.getF5().subtract(r060.getF5()));
		r080.setF6(r040.getF6().subtract(r060.getF6()));
		r080.setF7(r040.getF7().subtract(r060.getF7()));
		r080.setF8(r040.getF8().subtract(r060.getF8()));
		
		
		
		
		iline r090 =  new iline(); r090.setCode("r090");
		iline r091 = getDataICS3("r091", columns, "091", d1, d2,1); 
		iline r092 = getDataICS3("r092", columns, "092", d1, d2,1); 
		iline r093 = getDataICS3("r093", columns, "093", d1, d2,1); 
		iline r094 = getDataICS3("r094", columns, "094", d1, d2,1); 
		
//		r090 = r090.add(r091).add(r092).add(r093).add(r094);	

		
		iline r100 = new iline(); r100.setCode("r100"); 
		iline r101 = getDataICS3("r101", columns, "101", d1, d2,1); 
		iline r102 = getDataICS3("r102", columns, "102", d1, d2,1); 
		iline r103 = getDataICS3("r103", columns, "103", d1, d2,1); 
		iline r104 = getDataICS3("r104", columns, "104", d1, d2,1); 
		iline r105 = getDataICS3("r105", columns, "105", d1, d2,1); 
//		r100 = r100.add(r101).add(r102).add(r103).add(r104).add(r105);	
		r100.setF1(r100.getF1().add(r101.getF1()).add(r102.getF1()).add(r103.getF2()).add(r104.getF1()).add(r105.getF1())); 
		r100.setF2(r100.getF2().add(r101.getF2()).add(r102.getF2()).add(r103.getF2()).add(r104.getF2()).add(r105.getF2()));
		r100.setF3(r100.getF3().add(r101.getF3()).add(r102.getF3()).add(r103.getF2()).add(r104.getF3()).add(r105.getF3()));
		r100.setF4(r100.getF4().add(r101.getF4()).add(r102.getF4()).add(r103.getF2()).add(r104.getF4()).add(r105.getF4()));
		
		r100.setF5(r100.getF5().add(r101.getF5()).add(r102.getF5()).add(r103.getF2()).add(r104.getF5()).add(r105.getF5()));
		r100.setF6(r100.getF6().add(r101.getF6()).add(r102.getF6()).add(r103.getF2()).add(r104.getF6()).add(r105.getF6()));
		r100.setF7(r100.getF7().add(r101.getF7()).add(r102.getF7()).add(r103.getF2()).add(r104.getF7()).add(r105.getF7()));
		r100.setF8(r100.getF8().add(r101.getF8()).add(r102.getF8()).add(r103.getF2()).add(r104.getF8()).add(r105.getF8()));
		
		
		
		iline r110 = new iline(); r100.setCode("r100");
//		r110 = r090.subtract(r100);
		r110.setF1(r090.getF1().subtract(r100.getF1())); 
		r110.setF2(r090.getF2().subtract(r100.getF2()));
		r110.setF3(r090.getF3().subtract(r100.getF3()));
		r110.setF4(r090.getF4().subtract(r100.getF4()));
		
		r110.setF5(r090.getF5().subtract(r100.getF5()));
		r110.setF6(r090.getF6().subtract(r100.getF6()));
		r110.setF7(r090.getF7().subtract(r100.getF7()));
		r110.setF8(r090.getF8().subtract(r100.getF8()));
		
//		r140 = getDatabyAcct(d1, d2, "104%");
		iline r140 = getDataICS3("r105", columns, "105", d1, d2,2);
		
		
		

//		r130 = r130.add(r030).add(r080).add(r110);	r130p = r130p.add(r030p).add(r080p).add(r110p);
		iline r130 = new iline(); r130.setCode("r130");
		r130.setF1(r130.getF1().add(r030.getF1()).add(r080.getF1()).add(r110.getF1())); 
		r130.setF2(r130.getF2().add(r030.getF2()).add(r080.getF2()).add(r110.getF2()));
		r130.setF3(r130.getF3().add(r030.getF3()).add(r080.getF3()).add(r110.getF3()));
		r130.setF4(r130.getF4().add(r030.getF4()).add(r080.getF4()).add(r110.getF4()));
		
		r130.setF5(r130.getF5().add(r030.getF5()).add(r080.getF5()).add(r110.getF5()));
		r130.setF6(r130.getF6().add(r030.getF6()).add(r080.getF6()).add(r110.getF6()));
		r130.setF7(r130.getF7().add(r030.getF7()).add(r080.getF7()).add(r110.getF7()));
		r130.setF8(r130.getF8().add(r030.getF8()).add(r080.getF8()).add(r110.getF8()));
		
//		r150 = r150.add(r130).add(r140);	r150p = r150p.add(r130p).add(r140p);
		iline r150 = new iline(); r150.setCode("r150");
		r150.setF1(r150.getF1().add(r130.getF1()).add(r140.getF1())); 
		r150.setF2(r150.getF2().add(r130.getF2()).add(r140.getF2()));
		r150.setF3(r150.getF3().add(r130.getF3()).add(r140.getF3()));
		r150.setF4(r150.getF4().add(r130.getF4()).add(r140.getF4()));
		
		r150.setF5(r150.getF5().add(r130.getF5()).add(r140.getF5()));
		r150.setF6(r150.getF6().add(r130.getF6()).add(r140.getF6()));
		r150.setF7(r150.getF7().add(r130.getF7()).add(r140.getF7()));
		r150.setF8(r150.getF8().add(r130.getF8()).add(r140.getF8()));
		
		//-------------------------------------------------------------
		Line line;
		iline izero = new iline();
		
		/*		*/	line = copyiline(izero);	list.add(line);
		/*	010	*/	line = copyiline(r010);	list.add(line);
		/*		*/	line = copyiline(izero);	list.add(line);
		/*	011	*/	line = copyiline(r011);	list.add(line);
		/*	012	*/	line = copyiline(r012);	list.add(line);
		/*	013	*/	line = copyiline(r013);	list.add(line);
		/*	014	*/	line = copyiline(r014);	list.add(line);
		/*	015	*/	line = copyiline(r015);	list.add(line);
		/*	016	*/	line = copyiline(r016);	list.add(line);
		
		/*	020	*/	line = copyiline(r020);	list.add(line);
		/*		*/	line = copyiline(izero);	list.add(line);		
		/*	021	*/	line = copyiline(r021);	list.add(line);
		/*	022	*/	line = copyiline(r022);	list.add(line);
		/*	023	*/	line = copyiline(r023);	list.add(line);
		/*	024	*/	line = copyiline(r024);	list.add(line);
		/*	025	*/	line = copyiline(r025);	list.add(line);
		/*	026	*/	line = copyiline(r026);	list.add(line);
		/*	027	*/	line = copyiline(r027);	list.add(line);
		/*	030	*/	line = copyiline(r030);	list.add(line);
		
		/*		*/	line = copyiline(izero);	list.add(line);
		
		/*	040	*/	line = copyiline(r040);	list.add(line);
		/*		*/	line = copyiline(izero);	list.add(line);
		/*	041	*/	line = copyiline(r041);	list.add(line);
		/*	042	*/	line = copyiline(r042);	list.add(line);
		/*	043	*/	line = copyiline(r043);	list.add(line);
		/*	044	*/	line = copyiline(r044);	list.add(line);
		/*	045	*/	line = copyiline(r045);	list.add(line);
		/*	046	*/	line = copyiline(r046);	list.add(line);
		/*	047	*/	line = copyiline(r047);	list.add(line);
		/*	048	*/	line = copyiline(r048);	list.add(line);
		/*	049	*/	line = copyiline(r049);	list.add(line);
		/*	050	*/	line = copyiline(r050);	list.add(line);
		/*	051	*/	line = copyiline(r051);	list.add(line);
		
		/*	60	*/	line = copyiline(r060);	list.add(line);
		/*		*/	line = copyiline(izero);	list.add(line);		
		/*	61	*/	line = copyiline(r061);	list.add(line);
		/*	62	*/	line = copyiline(r062);	list.add(line);
		/*	63	*/	line = copyiline(r063);	list.add(line);
		/*	64	*/	line = copyiline(r064);	list.add(line);
		/*	65	*/	line = copyiline(r065);	list.add(line);
		/*	66	*/	line = copyiline(r066);	list.add(line);
		/*	67	*/	line = copyiline(r067);	list.add(line);
		/*	68	*/	line = copyiline(r068);	list.add(line);
		/*	69	*/	line = copyiline(r069);	list.add(line);
		/*	70	*/	line = copyiline(r070);	list.add(line);
		/*	71	*/	line = copyiline(r071);	list.add(line);

		/*	80	*/	line = copyiline(r080);	list.add(line);
		/*		*/	line = copyiline(izero);	list.add(line);		
		/*	90	*/	line = copyiline(r090);	list.add(line);		
		/*		*/	line = copyiline(izero);	list.add(line);
		/*	91	*/	line = copyiline(r091);	list.add(line);
		/*	92	*/	line = copyiline(r092);	list.add(line);
		/*	93	*/	line = copyiline(r093);	list.add(line);
		/*	94	*/	line = copyiline(r094);	list.add(line);
		/*	100	*/	line = copyiline(r100);	list.add(line);
		/*		*/	line = copyiline(izero);	list.add(line);
		/*	101	*/	line = copyiline(r101);	list.add(line);		
		/*	102	*/	line = copyiline(r102);	list.add(line);
		/*	103	*/	line = copyiline(r103);	list.add(line);
		/*	104	*/	line = copyiline(r104);	list.add(line);
		/*	105	*/	line = copyiline(r105);	list.add(line);
		/*	110	*/	line = copyiline(r110);	list.add(line);
		/*	120	*/	line = copyiline(izero);	list.add(line);
		/*	130	*/	line = copyiline(r130);	list.add(line);
		/*	140	*/	line = copyiline(r140);	list.add(line);
		/*	150	*/	line = copyiline(r150);	list.add(line);
		
		
		
		
		return list;
	}
	
	protected Line copyiline(iline l){
		Line line = new Line();
		
		line.setValue1(getDiv1000(l.getF1()).toString());
		line.setValue2(getDiv1000(l.getF2()).toString());
		line.setValue3(getDiv1000(l.getF3()).toString());
		line.setValue4(getDiv1000(l.getF4()).toString());
		
		line.setValue5(getDiv1000(l.getF5()).toString());
		line.setValue6(getDiv1000(l.getF6()).toString());
		line.setValue7(getDiv1000(l.getF7()).toString());
		line.setValue8(getDiv1000(l.getF8()).toString());
		
		return line;
	}
	
	protected iline getDataICS3(String rowName, List<MBCSFRInputFormColumn> columns, String codes, Date d1, Date d2, int function){
		BigDecimal summary = zero;
		iline line = new iline();
		line.setCode(rowName);
		int pos = 0;
		for(MBCSFRInputFormColumn column: columns){
			pos++;
			if(column.getC_BPartner_ID() != 0){
						
				BigDecimal v = zero;
				
				if(bp != null && column.getC_BPartner_ID() ==	bp.get_ID() && bp.getAD_OrgBP_ID_Int() > 0)
						v = zero;
				else{		
						if(function == 1)
							 v = getDataICS3_2(d1, d2, codes, column.getC_BPartner_ID());
						else
							 v = getDatabyAcctICS3(d1, d2, codes, column.getC_BPartner_ID());
				}
				
				switch(pos){
				case 1:
					line.setF1(v);
					break;
				case 2:
					line.setF2(v);
					break;
				case 3:
					line.setF3(v);
					break;
				case 4:
					line.setF4(v);
					break;
				case 5:
					line.setF5(v);
					break;
				case 6:
					line.setF6(v);
					break;
				case 7:
					line.setF7(v);
					break;
				case 8:
					line.setF8(v);
					break;
				}
				
				summary = summary.add(v);
			}
			
		}
		line.setF1(summary);
		return line;
		
	}

	private BigDecimal getDataICS3_2(Date dateFrom, Date dateTo, String code, int bp) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		BigDecimal result = new BigDecimal(0);
		
//		String[] words = codes.split(",");  
//		
//		String whereCodes = "";
//		if(words.length > 0){
//			whereCodes = " and ( ";
//			for(int i=0; i < words.length; i++){
//				if(i == (words.length-1))
//					whereCodes += "	ce.value like '"+words[i]+"%'	"; 
//				else
//					whereCodes += "	ce.value like '"+words[i]+"%' or ";
//					
//			}
//			whereCodes += " ) ";
//				
//		}
//
//		
//		String sql = 
//				  
//				  "			select coalesce(sum(fa.amtacctcr - fa.amtacctdr),0)	\n"
//				+ "			from fact_acct fa, c_elementvalue ce 	\n"
//				+ "			where fa.account_id = ce.c_elementvalue_id 	\n"
//				+ "			and fa.c_bpartner_id =  " + bp
//				;
//
//		if(startDate != null && endDate != null){	
//			sql +=	"		and fa.dateacct between '"+startDate+"'::date and '"+endDate+"'::date	\n"	;
//		}else if(endDate == null){
//			sql +=		"		and fa.dateacct <= '"+startDate+"'::date 	\n"	;
//		}if(startDate == null){
//			sql +=		"		and fa.dateacct <= '"+endDate+"'::date	\n"	;
//		}
//		
//			sql += whereCodes;				
		String sql = 
				" select(	\n"
				+ " 						(SELECT case when sum(l.stmtAmt)  is null then 0 else sum(l.stmtAmt)  end	\n"
				+ " 							FROM adempiere.c_bankstatement h	\n"
				+ " 							inner join adempiere.c_bankstatementline l on l.c_bankstatement_id = h.c_bankstatement_id	\n"
				+ " 							inner join fi_form3 f3 on f3.fi_form3_id = l.fi_form3_id	\n"
				+ " 							where h.statementdate between '" + dateFrom + "' and '"+ dateTo +"' 	\n"
				+ " 							and f3.fi_code = '" + code + "'	\n" 
//				+ " 							and f3.fi_form3_id not in (select fi_form3_id from adempiere.fi_form3exclude where isactive = 'Y' )  \n"
				+ "								and h.posted = 'Y'	\n"		
				+ "								and l.c_bpartner_id =  " + bp
				+ " 						)	\n"
				+ " 						+ 	\n"
				+ " 						(select	\n" 
				+ " 							case when sum(cp.payamt) is null then 0 else sum(cp.payamt) end		\n"
				+ " 							from c_payment cp 		\n"
				+ " 							inner join fi_form3 f3 on f3.fi_form3_id = cp.fi_form3_id	\n"	 
				+ " 							where dateacct between '" + dateFrom + "' and '"+ dateTo +"' 		\n"
				+ " 							and f3.fi_code = '" + code + "'	\n"
//				+ " 							and f3.fi_form3_id not in (select fi_form3_id from adempiere.fi_form3exclude where isactive = 'Y' )  \n"
				+ "								and cp.posted = 'Y'	\n"	
				+ "								and cp.c_bpartner_id =  " + bp
				+ " 						)	\n"
				+ " 						+	\n"
				+ " 						(	\n"
				+ " 						select case when sum(l.amtsourcedr) is null then 0 else sum(l.amtsourcedr) end	\n"
				+ " 							from adempiere.gl_journal h	\n"
				+ " 							inner join adempiere.gl_journalline l on l.gl_journal_id = h.gl_journal_id	\n"
				+ " 							inner join fi_form3 f3 on f3.fi_form3_id = l.fi_form3_id 	\n"
				+ "								left join adempiere.c_validcombination c on c.c_validcombination_id = l.c_validcombination_id	\n"
				+ " 							where h.dateacct between '" + dateFrom + "' and '"+ dateTo +"'	\n"
				+ " 							and f3.fi_code = '" + code + "'	\n"
//				+ " 							and f3.fi_form3_id not in (select fi_form3_id from adempiere.fi_form3exclude where isactive = 'Y' )  \n"
				+ "								and h.posted = 'Y'	\n"			
				+ "								and c.c_bpartner_id =  " + bp
				+ " 						)	\n"
				+ " 					) amt	\n"
				;
		
		

		try {
			pstmt = DB.prepareStatement(sql, null);

			rs = pstmt.executeQuery();
			if (rs.next()) {
				result = rs.getBigDecimal(1);
//				if(result.compareTo(Env.ZERO) != 0){
//						result = result.divide(new BigDecimal(1000));
//						result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
//				}
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "Report: FI_Form4", e);
			return new BigDecimal(0);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
		return result;
		
	}
	
	private BigDecimal getDatabyAcctICS3(Date d, Date d2, String code, int C_BPartner_ID) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		BigDecimal result = zero;
		StringBuilder sql = new StringBuilder();
		
		sql.append(
				  " select coalesce(sum(fa.amtacctdr - fa.amtacctcr),0) from fact_acct fa, c_elementvalue ce " 
				+ " where fa.account_id=ce.c_elementvalue_id	"
				+ " and PostingType='A' "		  
				+ " and fa.dateacct < '"+d+"' and ce.value like '"+code+"'"
				+ " and fa.c_bpartner_id = " + C_BPartner_ID 
				);				


		try {
			pstmt = DB.prepareStatement(sql.toString(), null);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				result = rs.getBigDecimal(1);
			}

			if(result == null) 
				result = zero;
//			if(result.compareTo(zero) != 0 )
//				result = result.divide(new BigDecimal(1000));
//			
//			result = result.setScale(0, BigDecimal.ROUND_HALF_UP);

		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "Report: FI_Form3Direct", e);
//			getSQlresult = e.getMessage();
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
		return result; 
	}	
	

	private List<Line> load7(MBCSFRInputFormCompany company) {
 		List<Line> list = new ArrayList<Line>();
		
		MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
		int C_Period_ID = period.getPeriodTo();
		MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
		Long time;

		Timestamp ts1 = p.getEndDate();//begin of choosen period 
		//ts2
		Timestamp ts2 = p.getEndDate();//choosen period 
		Timestamp ts3 = p.getEndDate();//begin of choosen period - year 
		Timestamp ts4 = p.getEndDate();//choosen period - year
		
		Timestamp tsEndDate = new Timestamp(0);
		Timestamp tsStartDate = new Timestamp(0);
		Timestamp tsEndDateMinusYear = new Timestamp(0);
		Timestamp tsStartDateMinusYear = new Timestamp(0);
		
		
		int year = p.getEndDate().getYear() + 1900;

		if(year == 2013){
			
			Timestamp ts = p.getEndDate();
			
			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(ts.getTime());
			c.add(Calendar.YEAR, -1);
			c.set(Calendar.DAY_OF_MONTH, 1);
			c.set(Calendar.MONTH, 0);
			tsStartDateMinusYear.setTime(c.getTimeInMillis());
			
			c = Calendar.getInstance();
			c.setTimeInMillis(ts.getTime());
			c.add(Calendar.YEAR, -1);
			tsEndDateMinusYear.setTime(c.getTimeInMillis());

			c = Calendar.getInstance();
			c.setTimeInMillis(ts.getTime());
//			c.add(Calendar.YEAR, 1);
			c.set(Calendar.DAY_OF_MONTH, 1);
			c.set(Calendar.MONTH, 0);
			tsStartDate.setTime(c.getTimeInMillis());
			
			c = Calendar.getInstance();
			c.setTimeInMillis(ts.getTime());
//			c.add(Calendar.YEAR, 1);
			tsEndDate.setTime(c.getTimeInMillis());
			int i = 1;
			
		}else{
			
			Timestamp ts = p.getEndDate();
			
			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(ts.getTime());
			c.set(Calendar.DAY_OF_MONTH, 1);
			c.set(Calendar.MONTH, 0);
			tsStartDate.setTime(c.getTimeInMillis());

			c = Calendar.getInstance();
			c.setTimeInMillis(ts.getTime());
			c.set(Calendar.DAY_OF_MONTH, 1);
			c.set(Calendar.MONTH, 0);
			c.add(Calendar.YEAR, -1);
			tsStartDateMinusYear.setTime(c.getTimeInMillis());

			c = Calendar.getInstance();
			c.setTimeInMillis(ts.getTime());
			c.add(Calendar.YEAR, -1);
			tsEndDateMinusYear.setTime(c.getTimeInMillis());
			
			c = Calendar.getInstance();
			c.setTimeInMillis(ts.getTime());
//			c.add(Calendar.YEAR, 1);
			tsEndDate.setTime(c.getTimeInMillis());
//			
//			Calendar c = Calendar.getInstance();
//			c.setTimeInMillis(ts.getTime());
//			c.add(Calendar.YEAR, -1);
//			c.set(Calendar.DAY_OF_MONTH, 1);
//			c.set(Calendar.MONTH, 0);
//			tsStartDateMinusYear.setTime(c.getTimeInMillis());
//			
//			c = Calendar.getInstance();
//			c.setTimeInMillis(ts.getTime());
//			c.add(Calendar.YEAR, -1);
//			tsEndDateMinusYear.setTime(c.getTimeInMillis());
//
//			c = Calendar.getInstance();
//			c.setTimeInMillis(ts.getTime());
////			c.add(Calendar.YEAR, 1);
//			c.set(Calendar.DAY_OF_MONTH, 1);
//			c.set(Calendar.MONTH, 0);
//			tsStartDate.setTime(c.getTimeInMillis());
//			
//			c = Calendar.getInstance();
//			c.setTimeInMillis(ts.getTime());
////			c.add(Calendar.YEAR, 1);
//			tsEndDate.setTime(c.getTimeInMillis());
//			int i = 1;
			
		}		
		
		//ts1
//		Date d2 = new Date(ts2.getTime());
//		Calendar c = Calendar.getInstance();
//		c.setTime(new Date(ts2.getTime()));
//		c.set(Calendar.DAY_OF_MONTH, 1);
//		c.set(Calendar.MONTH, 0);
//		Date d1 = new Date(c.getTimeInMillis());
//
//		//ts3
//		c.setTime(d1);
//		c.add(Calendar.YEAR, -1);
//		Date d3 = new Date(c.getTimeInMillis());
//		
//		//ts4
//		c.setTime(p.getEndDate());
//		c.add(Calendar.YEAR, -1);
//		Date d4 = new Date(c.getTimeInMillis());
		
		String codes = "";
		
//		BigDecimal r5000 = zero; BigDecimal r5000p = zero;
//		BigDecimal r5100 = zero; BigDecimal r5100p = zero;
//		BigDecimal r5200 = zero; BigDecimal r5200p = zero;
//		BigDecimal r5300 = zero; BigDecimal r5300p = zero;
//		BigDecimal r543000 = zero; BigDecimal r543000p = zero;
//		BigDecimal r541000 = zero; BigDecimal r541000p = zero;
//		BigDecimal r011 = zero; BigDecimal r011p = zero;
//		BigDecimal r012 = zero; BigDecimal r012p = zero;
//		BigDecimal r013 = zero; BigDecimal r013p = zero;
//		BigDecimal r014 = zero; BigDecimal r014p = zero;
//		BigDecimal r015 = zero; BigDecimal r015p = zero;
	
		
//		codes = "50";
//		r5000 = getData4(codes, d1, d2, "dr");	r5000p = getData4(tsStartDateMinusYear, null, "50");
//
//		codes = "5100";
//		r5100 = getData4(codes, d1, d2, "cr");	r5100p = getData4(tsStartDateMinusYear, null, "5100");
//
//		codes = "5200";
//		r5200 = getData4(codes, d1, d2, "cr");	r5200p = getData4(tsStartDateMinusYear, null, "5200");
//
//		codes = "5300";
//		r5300 = getData4(codes, d1, d2, "cr");	r5300p = getData4(tsStartDateMinusYear, null, "5300");
//		
//		codes = "541";
//		r541000 = getData4(codes, d1, d2, "cr");	r541000p = getData4(tsStartDateMinusYear, null, "541,543");
//		
//		codes = "543";
//		r543000 = getData4(codes, d1, d2, "dr");	r543000p = getData4(codes, d3, d4, "dr");
//		BigDecimal tmp = r541000; 
//		r541000 = r541000.subtract(r543000);		r541000p = r541000p.subtract(r543000p);
//
//		r543000 = getNew541(new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//		r543000 = r543000.subtract(tmp);
//		r543000p = getNew541(new Timestamp(d3.getTime()), new Timestamp(d4.getTime()));
		
//		prev year
		
		BigDecimal v3030prev = getData2("303", tsStartDateMinusYear, tsEndDateMinusYear, "cr").negate();
		
		BigDecimal v5000prev = Env.ZERO;

		if(year != 2013)
			v5000prev = getData7(tsStartDateMinusYear, null, "50"); 		

		BigDecimal v5100prev = getData7(tsStartDateMinusYear, null, "5100"); 
		
		BigDecimal v5200prev = getData7(tsStartDateMinusYear, null, "5200"); 
		
		BigDecimal v5300prev = getData7(tsStartDateMinusYear, null, "5300"); 
		
		BigDecimal tmpprev = Env.ZERO;
		BigDecimal v543_1000prev = Env.ZERO;
		if(year != 2013)
			v543_1000prev = getData7(tsStartDateMinusYear, null, "541,543"); 

 		BigDecimal new1prev = getData7(tsStartDateMinusYear, tsEndDateMinusYear, "50");
//		BigDecimal new2prev = getData7(tsStartDateMinusYear, tsEndDateMinusYear, "541,543");		
		BigDecimal new2prev0 = getData2("6", tsStartDateMinusYear, tsEndDateMinusYear, "cr");
		BigDecimal new2prev1 = getData2("7", tsStartDateMinusYear, tsEndDateMinusYear, "dr");
		BigDecimal new2prev = new2prev0.subtract(new2prev1);
		
		
//		current year-------------------------------------------------------------------------------------
		
		BigDecimal v3030 = getData2("303", tsStartDate, tsEndDate, "cr").negate();
		
		BigDecimal v5000 = getData7(tsStartDate, null, "50"); 
		
		BigDecimal v5100 = getData7(tsStartDate, null, "5100"); 
		
		BigDecimal v5200 = getData7(tsStartDate, null, "5200"); 
		
		BigDecimal v5300 = getData7(tsStartDate, null, "5300"); 
		
		BigDecimal v541000 = getData7(tsStartDate, null, "541,543"); 

//		BigDecimal v543000 = getData7(tsStartDate, tsEndDate, "541,543"); 
		BigDecimal v6000 = getData2("6", tsStartDate, tsEndDate, "cr");
		BigDecimal v7000 = getData2("7", tsStartDate, tsEndDate, "dr");
		BigDecimal v6_7 = v6000.subtract(v7000);
		
		BigDecimal new541 = getData7(tsStartDate, tsEndDate, "50"); 
		
		BigDecimal total = Env.ZERO;
		BigDecimal totalp = Env.ZERO;
		total = total.add(v5000).add(v5100).add(v5200).add(v5300).add(v541000);
		totalp = totalp.add(v5000prev).add(v5100prev).add(v5200prev).add(v5300prev).add(v543_1000prev);
		
		
		BigDecimal total541000 = Env.ZERO;
		BigDecimal total543000 = Env.ZERO;
		
		total541000 = total541000.add(v541000);
		
		total543000 = total543000.add(v5000).add(total541000);
		
		Line line;
		
//		/*	010	*/	line = new Line();		line.setValue1(r010.toString());	line.setValue2(r010p.toString());	list.add(line);
		
		/*	010	*/	
					BigDecimal r010 = Env.ZERO;
					r010 = r010.add(v5000prev).add(v5100prev).add(v5200prev).add(v5300prev).add(v543_1000prev);
					line = new Line();		
						line.setValue1(getDiv1000(v5000prev).toString()); 
						line.setValue2(getDiv1000(v5100prev).toString()); 
						line.setValue3(getDiv1000(v5200prev).toString()); 
						line.setValue4(getDiv1000(v5300prev).toString()); 
						line.setValue5(getDiv1000(v543_1000prev).toString()); 
						line.setValue6(zero.toString()); 
						line.setValue7(getDiv1000(r010).toString());	
					list.add(line);
		/*	011	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);		
		/*	100	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);

		
		/*	200	*/	BigDecimal r200 = Env.ZERO;
					r200 = r200.add(new1prev).add(new2prev);
					line = new Line();		
						line.setValue1(getDiv1000(new1prev).toString()); 
						line.setValue2(zero.toString()); 
						line.setValue3(zero.toString()); 
						line.setValue4(zero.toString()); 
						line.setValue5(getDiv1000(new2prev).toString()); 
						line.setValue6(zero.toString()); 
						line.setValue7(getDiv1000(r200).toString());	
					list.add(line);

		/*	210	*/	BigDecimal r210 = Env.ZERO;
					r210 = r210.add(new1prev).add(new2prev);
					line = new Line();		
						line.setValue1(getDiv1000(new1prev).toString()); 
						line.setValue2(zero.toString()); 
						line.setValue3(zero.toString()); 
						line.setValue4(zero.toString()); 
						line.setValue5(getDiv1000(new2prev).toString()); 
						line.setValue6(zero.toString()); 
						line.setValue7(getDiv1000(r210).toString());	
					list.add(line);
					
//		/*	210	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	220	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	221	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	222	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	223	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	224	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	225	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	226	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	227	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	228	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	229	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	300	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	310	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	311	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	312	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	312	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	313	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	314	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		
		/*	315	*/ 	BigDecimal r315 = Env.ZERO;
					r315 = r315.add(v3030prev);
					line = new Line();		
						line.setValue1(zero.toString());
						line.setValue2(zero.toString()); 
						line.setValue3(zero.toString()); 
						line.setValue4(zero.toString()); 
						line.setValue5(getDiv1000(r315).toString());  
						line.setValue6(zero.toString()); 
						line.setValue7(getDiv1000(r315).toString());	
					list.add(line);
		
		/*	316	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	317	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	318	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		
		/*	400	*/	BigDecimal r400 = Env.ZERO;
					r400 = r400.add(v5000).add(v5100).add(v5200).add(v5300).add(v541000);
					line = new Line();		
						line.setValue1(getDiv1000(v5000).toString()); 
						line.setValue2(getDiv1000(v5100).toString()); 
						line.setValue3(getDiv1000(v5200).toString()); 
						line.setValue4(getDiv1000(v5300).toString()); 
						line.setValue5(getDiv1000(v541000).toString()); 
						line.setValue6(zero.toString()); 
						line.setValue7(getDiv1000(r400).toString());	
					list.add(line);
		
		/*	401	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	500	*/	BigDecimal r500 = Env.ZERO;
					r500 = r500.add(v5000).add(v5100).add(v5200).add(v5300).add(v541000);
					line = new Line();		
						line.setValue1(getDiv1000(v5000).toString()); 
						line.setValue2(getDiv1000(v5100).toString()); 
						line.setValue3(getDiv1000(v5200).toString()); 
						line.setValue4(getDiv1000(v5300).toString()); 
						line.setValue5(getDiv1000(v541000).toString()); 
						line.setValue6(zero.toString()); 
						line.setValue7(getDiv1000(r500).toString());	
					list.add(line);
		/*	600	*/	BigDecimal r600 = Env.ZERO;
					r600 = r600.add(v6_7).add(new541);	
					line = new Line();		
						line.setValue1(getDiv1000(new541).toString()); 
						line.setValue2(zero.toString()); 
						line.setValue3(zero.toString()); 
						line.setValue4(zero.toString()); 
						line.setValue5(getDiv1000(v6_7).toString()); 
						line.setValue6(zero.toString()); 
						line.setValue7(getDiv1000(r600).toString());	
					list.add(line);
					
		/*	610	*/	BigDecimal r610 = Env.ZERO;
					r610 = r610.add(v6_7).add(new541);	
					line = new Line();		
						line.setValue1(getDiv1000(new541).toString()); 
						line.setValue2(zero.toString()); 
						line.setValue3(zero.toString()); 
						line.setValue4(zero.toString()); 
						line.setValue5(getDiv1000(v6_7).toString()); 
						line.setValue6(zero.toString()); 
						line.setValue7(getDiv1000(r610).toString());	
					list.add(line);
		
		/*	620	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	621	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	622	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	623	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	624	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	625	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	626	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	627	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	628	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	629	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		
		/*	700	*/	BigDecimal r700 = Env.ZERO;
					r700 = r700.add(v3030);
					line = new Line();		
						line.setValue1(zero.toString()); 
						line.setValue2(zero.toString()); 
						line.setValue3(zero.toString()); 
						line.setValue4(zero.toString()); 
						line.setValue5(getDiv1000(v3030).toString()); 
						line.setValue6(zero.toString()); 
						line.setValue7(getDiv1000(r700).toString());	
					list.add(line);
					
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	710	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*		*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	711	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	712	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	713	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	714	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		
		/*	715	*/	BigDecimal r715 = Env.ZERO;
					r715 = r715.add(v3030);
					line = new Line();		
						line.setValue1(zero.toString()); 
						line.setValue2(zero.toString()); 
						line.setValue3(zero.toString()); 
						line.setValue4(zero.toString()); 
						line.setValue5(getDiv1000(v3030).toString()); 
						line.setValue6(zero.toString()); 
						line.setValue7(getDiv1000(r715).toString());	
					list.add(line);
		
		/*	716	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	717	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	718	*/	line = new Line();		line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString()); line.setValue7(zero.toString());	list.add(line);
		/*	800	*/	
					line = new Line();		
						BigDecimal v5000_new541 = v5000.add(new541);
						line.setValue1(getDiv1000(v5000_new541).toString()); 
						line.setValue2(zero.toString()); 
						line.setValue3(zero.toString()); 
						line.setValue4(zero.toString()); 
						
						BigDecimal c5 = Env.ZERO;
						c5 = c5.add(v541000).add(v6_7).add(v3030); 
						line.setValue5(getDiv1000(c5).toString()); 
						
						line.setValue6(zero.toString());
						
						BigDecimal c7 = Env.ZERO;
						c7 = c7.add(v5000).add(c5).add(new541);
						line.setValue7(getDiv1000(c7).toString());	
					list.add(line);

		return list;

	}

	private BigDecimal getData7(Timestamp startDate, Timestamp endDate, String codes) {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		BigDecimal result = new BigDecimal(0);
		
		String[] words = codes.split(",");  
		
		String whereCodes = "";
		if(words.length > 0){
			whereCodes = " and ( ";
			for(int i=0; i < words.length; i++){
				if(i == (words.length-1))
					whereCodes += "	ce.value like '"+words[i]+"%'	"; 
				else
					whereCodes += "	ce.value like '"+words[i]+"%' or ";
					
			}
			whereCodes += " ) ";
				
		}

		
		String sql = 
				  
				  "			select coalesce(sum(fa.amtacctcr - fa.amtacctdr),0)	\n"
				+ "			from fact_acct fa, c_elementvalue ce 	\n"
				+ "			where fa.account_id = ce.c_elementvalue_id 	\n"
				;

		if(startDate != null && endDate != null){	
			sql +=	"		and fa.dateacct between '"+startDate+"'::date and '"+endDate+"'::date	\n"	;
		}else if(endDate == null){
			sql +=		"		and fa.dateacct <= '"+startDate+"'::date 	\n"	;
		}if(startDate == null){
			sql +=		"		and fa.dateacct <= '"+endDate+"'::date	\n"	;
		}
		
			sql += whereCodes;				

		

		try {
			pstmt = DB.prepareStatement(sql, null);

			rs = pstmt.executeQuery();
			if (rs.next()) {
				result = rs.getBigDecimal(1);
//				if(result.compareTo(Env.ZERO) != 0){
//						result = result.divide(new BigDecimal(1000));
//						result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
//				}
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "Report: FI_Form4", e);
			return new BigDecimal(0);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
		return result;
		
	}
	
	
//	private BigDecimal getNew541(Timestamp startDate, Timestamp endDate) {
//		PreparedStatement pstmt = null;
//		ResultSet rs = null;
//		BigDecimal n541 = zero;
//		String sql = 
//				  
//				  "			select case when (sum(fa.amtacctcr - fa.amtacctdr)) is null then 0 else (sum(fa.amtacctcr - fa.amtacctdr)) end	\n"
//				+ "			from fact_acct fa, c_elementvalue ce 	\n"
//				+ "			where fa.account_id = ce.c_elementvalue_id 	\n"
//				+ "						and fa.dateacct between '" + startDate + "' and '"+ endDate +"'	\n"
//				+ "						and ce.value like '541%'" 
//				;
//			
//
//		try {
//			pstmt = DB.prepareStatement(sql, null);
//
//			rs = pstmt.executeQuery();
//			while (rs.next()) {
//				n541 = rs.getBigDecimal(1);
//			}
//		} catch (SQLException e) {
//			s_log.log(Level.SEVERE, "Report: FI_Form4", e);
////			getSQlresult = e.getMessage();
//			return zero;
//		} finally {
//			DB.close(rs, pstmt);
//			rs = null;
//			pstmt = null;
//		}
//		
//		if(n541.compareTo(zero) != 0 )
//			n541 = n541.divide(new BigDecimal(1000));
//		
//		n541 = n541.setScale(0, BigDecimal.ROUND_HALF_UP);		
//		return n541;
//		
//	}
//	
//	
//	private BigDecimal getData4(String codes, Date dateFrom, Date dateTo, String type){
//		BigDecimal result = zero;
//		StringBuffer sql = new StringBuffer();
//		
//		String[] words = codes.split(",");  
//		
//		String whereCodes = "";
//		if(words.length > 0){
//			whereCodes = " and ( ";
//			for(int i=0; i < words.length; i++){
//				if(i == (words.length-1))
//					whereCodes += "	ce.value like '"+words[i]+"%'	"; 
//				else
//					whereCodes += "	ce.value like '"+words[i]+"%' or ";
//					
//			}
//			whereCodes += " ) ";
//				
//		}
//
////		//prepare sql
//		if(whereCodes.equals(""))
//			return result;
//		
//		if(type.equals("dr"))
//			if(codes.equals("50"))
//				sql.append("	select coalesce(sum(fa.amtacctcr - fa.amtacctdr),0)	\n");
//			else	
//				sql.append("	select coalesce(sum(fa.amtacctdr),0)	\n");
//		else
//			sql.append("	select coalesce(sum(fa.amtacctcr),0)	\n");
//			
//		sql.append(
//				"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n"	+ 
//				"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	+	
//				"		" + whereCodes
//				);
//		
//		if(codes.equals("541") || codes.equals("543"))
//			sql.append(
//			 "				group by fa.dateacct " 			
//			+ "				order by fa.dateacct " 
//			+ " 			OFFSET 0 FETCH FIRST 1 ROWS ONLY		" 
//		);
//		
//		PreparedStatement pstmt = null;
//		ResultSet rs = null;
//		
//		try {
//			pstmt = DB.prepareStatement(sql.toString(), null);
////			pstmt = DB.prepareStatement(s, null);
////			System.out.println(sql);
//			rs = pstmt.executeQuery();
//			while (rs.next()) {
//					result = rs.getBigDecimal(1);
//			}
//		} catch (SQLException e) {
//			s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
//		} finally {
//			DB.close(rs, pstmt);
//			rs = null;
//			pstmt = null;
//		}
//		
//		if(result.compareTo(zero) != 0 )
//			result = result.divide(new BigDecimal(1000));
//		
//		result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
//		
//		return result; 
//	}	

	private List<Line> load9(MBCSFRInputFormCompany company) {
 		List<Line> list = new ArrayList<Line>();
		
		MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
		int C_Period_ID = period.getPeriodFrom();
		MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);

		int C_Period_ID2 = period.getPeriodTo();
		MPeriod p2 = new MPeriod(Env.getCtx(), C_Period_ID2, null);

		Date d1 = p.getStartDate();
		Date d2 = p2.getEndDate();
		
		BigDecimal r13 = zero; BigDecimal r14 = zero;	BigDecimal r110 = zero;	BigDecimal r111 = zero;	BigDecimal r112 = zero; BigDecimal r113 = zero;	
		
		BigDecimal r43 = zero;	BigDecimal r44 = zero;	BigDecimal r410 = zero;
			BigDecimal r53 = zero;	BigDecimal r54 = zero;	BigDecimal r510 = zero;
			BigDecimal r63 = zero;	BigDecimal r64 = zero; BigDecimal r610 = zero;
			BigDecimal r73 = zero;	BigDecimal r74 = zero; BigDecimal r710 = zero;
			BigDecimal r83 = zero;	BigDecimal r84 = zero; BigDecimal r810 = zero;
		
		BigDecimal r93 = zero;	BigDecimal r94 = zero; BigDecimal r910 = zero;	
			BigDecimal r103 = zero;	BigDecimal r104 = zero; BigDecimal r1010 = zero;
		
		BigDecimal r143 = zero;	BigDecimal r144 = zero; BigDecimal r1410 = zero;	BigDecimal r144_2 = zero;	BigDecimal r1410_2 = zero;	
			BigDecimal r153 = zero;	BigDecimal r154 = zero; BigDecimal r1510 = zero;
			BigDecimal r163 = zero;	BigDecimal r164 = zero; BigDecimal r1610 = zero;
			BigDecimal r173 = zero;	BigDecimal r174 = zero; BigDecimal r1710 = zero;
			BigDecimal r183 = zero;	BigDecimal r184 = zero; BigDecimal r1810 = zero;
			BigDecimal r193 = zero;	BigDecimal r194 = zero; BigDecimal r1910 = zero;
			BigDecimal r203 = zero;	BigDecimal r204 = zero; BigDecimal r2010 = zero;
		
		BigDecimal r243 = zero;	BigDecimal r244 = zero; BigDecimal r2410 = zero;
			BigDecimal r253 = zero;	BigDecimal r254 = zero; BigDecimal r2510 = zero;
			BigDecimal r263 = zero;	BigDecimal r264 = zero; BigDecimal r2610 = zero;
			BigDecimal r273 = zero;	BigDecimal r274 = zero; BigDecimal r2710 = zero;
		
		
		BigDecimal r323 = zero;	BigDecimal r324 = zero; BigDecimal r3210 = zero;	
			BigDecimal r333 = zero;						BigDecimal r3310 = zero;
			BigDecimal r343 = zero;	BigDecimal r344 = zero; BigDecimal r3410 = zero;
			BigDecimal r353 = zero;	BigDecimal r3510 = zero;
			BigDecimal r363 = zero;	BigDecimal r364 = zero; BigDecimal r3610 = zero;
			BigDecimal r383 = zero;	BigDecimal r3810 = zero;
			BigDecimal r393 = zero;	BigDecimal r394 = zero; BigDecimal r3910 = zero;
			
		BigDecimal r413 = zero;	BigDecimal r414 = zero; BigDecimal r4110 = zero;
			BigDecimal r403 = zero;	BigDecimal r404 = zero; BigDecimal r4010 = zero;	
			BigDecimal r1013 = zero;	BigDecimal r1014 = zero; BigDecimal r10110 = zero;
			BigDecimal r1023 = zero;	BigDecimal r1024 = zero; BigDecimal r10210 = zero;
		
		
		BigDecimal r1063 = zero;	BigDecimal r1064 = zero; BigDecimal r10610 = zero;	BigDecimal r10611 = zero;	BigDecimal r10612 = zero;	
		BigDecimal r1063_2 = zero;	BigDecimal r1064_2 = zero; BigDecimal r10610_2 = zero;	BigDecimal r10611_2 = zero;	BigDecimal r10612_2 = zero;
			BigDecimal r1073 = zero;	BigDecimal r10710 = zero;
			BigDecimal r1083 = zero;		BigDecimal r10810 = zero;
			BigDecimal r1113 = zero;	BigDecimal r1114 = zero; BigDecimal r11110 = zero;
			BigDecimal r1153 = zero;									BigDecimal r11511 = zero; 
			BigDecimal r1193 = zero;													BigDecimal r11912 = zero;
			BigDecimal r1213 = zero;													BigDecimal r12112 = zero;
																						
			BigDecimal r1223 = zero;	BigDecimal r1224 = zero; BigDecimal r12210 = zero;
		
		BigDecimal r1243 = zero;	BigDecimal r1244 = zero; BigDecimal r12410 = zero;	BigDecimal r12412 = zero;	BigDecimal r12413 = zero;
			BigDecimal r1333 = zero;	BigDecimal r1334 = zero; BigDecimal r13310 = zero;	BigDecimal r13312 = zero; 
			BigDecimal r1343 = zero;																						BigDecimal r13413 = zero;		
		

	//--------------													
																											
	//tablesWithC_charge_IDs = initListOfTablesWithC_Charge_id();																										
																											
		r54 = getData9("1000000",d1,d2);	r510 = getData9("1000043",d1,d2);	r53 = r53.add(r54).add(r510);	
		r64 = getData9("1000002",d1,d2); 	r610 = getData9("1000044",d1,d2);	r63 = r63.add(r64).add(r610);	
		r74 = getData9("1000003",d1,d2);  r710 = getData9("1000045",d1,d2);		r73 = r73.add(r74).add(r710);	
		r84 = getData9("1000004,1000005",d1,d2);  r810 = getData9("1000047,10000046",d1,d2);	r83 = r83.add(r84).add(r810);	
		r44 = r44.add(r54).add(r64).add(r74).add(r84);	r410 = r410.add(r510).add(r610).add(r710).add(r810);	r43 = r43.add(r44).add(r410);	
		
		r104 = getData9("1000006,1000007,1000008",d1,d2);  r1010 = getData9("1000048,1000049,1000050",d1,d2);	r103 = r103.add(r104).add(r1010);	
	r94 = r104;	r910 = r1010;	r93 = r93.add(r94).add(r910);	
		
		r154 = getData9("1000009",d1,d2);  r1510 = getData9("1000051",d1,d2);	r153 = r153.add(r154).add(r1510);	
		r164 = getData9("1000010",d1,d2);  r1610 = getData9("1000052",d1,d2);	r163 = r163.add(r164).add(r1610);	
		r174 = getData9("1000029",d1,d2);  r1710 = getData9("1000070",d1,d2);	r173 = r173.add(r174).add(r1710);	
		r184 = getData9("1000028",d1,d2);  r1810 = getData9("1000069",d1,d2);	r183 = r183.add(r184).add(r1810);	
		r194 = getData9("1000039, 1000033, 1000040",d1,d2);  r1910 = getData9("1000109,1000042,1000074,1000110",d1,d2);	r193 = r193.add(r194).add(r1910);	
		r204 = getData9("1000018",d1,d2);  r2010 = getData9("1000058,1000083,1000084",d1,d2);	r203 = r203.add(r204).add(r2010);	
	r144 = r144.add(r154).add(r164);							r1410 = r1410.add(r1510).add(r1610);
	r144_2 = r144_2.add(r174).add(r184).add(r194).add(r204);	r1410_2 = r1410_2.add(r1710).add(r1810).add(r1910).add(r2010);
	r143 = r143.add(r144).add(r1410);	
		
		r244 = getData9("1000025,1000065,1000098",d1,d2);	//r243 = r243.add(r244).add(r2410);	
		r254 = getData9("1000019,1000020",d1,d2);  r2510 = getData9("1000059,1000060,1000092,1000093",d1,d2);	r253 = r253.add(r254).add(r2510);	
		r264 = getData9("1000021,1000022",d1,d2);  r2610 = getData9("1000061,1000062,1000094,10000095",d1,d2);	r263 = r263.add(r264).add(r2610);	
		r274 = getData9("1000023,1000027",d1,d2);  r2710 = getData9("1000063,1000064,1000096,1000097",d1,d2);	r273 = r273.add(r274).add(r2710);	
	r244 = r244.add(r254).add(r264).add(r274);		r2410 = r2410.add(r2510).add(r2610).add(r2710);		r243 = r243.add(r244).add(r2410);	
	
	
												r3310 = getData9("1000078",d1,d2);	r333 = r333.add(r3310);
		r344 = getData9("1000026,1000027",d1,d2);  			r3410 = getData9("1000067,1000068",d1,d2);	r343 = r343.add(r344).add(r3410);	
		r3510 = getData9("1000066,1000100",d1,d2);	r353 = r353.add(r3510);							
		r364 = getData9("1000030,1000031,1000032",d1,d2);  	r3610 = getData9("1000071,1000072,1000073",d1,d2);	r363 = r363.add(r364).add(r3610);	
		
		r3810 = getData9("1000086",d1,d2);	r383 = r383.add(r3810);							
		r394 = getData9("1000034",d1,d2);  					r3910 = getData9("1000075,1000099",d1,d2);	r393 = r393.add(r394).add(r3910);	
		r324 = r324.add(r344).add(r364).add(r394);	r3210 = r3210.add(r3310).add(r3410).add(r3510).add(r3610).add(r3810).add(r3910);	
		r323 = r323.add(r324).add(r3210);	
	
		r414 = getData9("1000011",d1,d2);  					r4110 = getData9("1000053",d1,d2);	r413 = r413.add(r414).add(r4110);	
		r1014 = getData9("1000013",d1,d2);  				r10110 = getData9("1000055",d1,d2);	r1013 = r1013.add(r1014).add(r10110);	
		r1024 = getData9("1000012",d1,d2);  				r10210 = getData9("1000054",d1,d2);	r1023 = r1023.add(r1024).add(r10210);	
	r404 = r404.add(r414).add(r1014).add(r1024);	r4010 = r4010.add(r4110).add(r10110).add(r10210);	r403 = r403.add(r404).add(r4010);	
	
		r10710 = getData9("1000079",d1,d2);	r1073 = r1073.add(r10710);						
		r10810 = getData9("1000076,1000077",d1,d2);	r1083 = r1083.add(r10810);							
		r1114 = getData9("1000036",d1,d2);					r11110 = getData9("1000080",d1,d2);	r1113 = r1113.add(r1114).add(r11110);	
		r11511 = getData9("1000090",d1,d2);	r1153 = r1153.add(r11511);								 
		r11912 = getData9("1000089",d1,d2);	r1193 = r1193.add(r11912);								
		r12112 = getData9("1000088",d1,d2);	r1213 = r1213.add(r12112);																																
	
		r1224 = getData9("1000035",d1,d2);  r12210 = getData9("1000083",d1,d2);	r1223 = r1223.add(r1224).add(r12210);	
		
			r1064 = r1064;	r10610 = r10610.add(r10710).add(r10810);	
//			r10611 = r10611.add(r11511);	r10612 = r10612.add(r11912).add(r12112);
			r1063 = r1063.add(r1064).add(r10610).add(r10611).add(r10612); 
	 
		r1064_2 = r1064_2.add(r1114).add(r1224);	r10610_2 = r10610_2.add(r11110).add(r12210);	r10611_2 = r10611.add(r11511); r10612_2 = r10612_2.add(r11912).add(r12112);
		r1063_2 = r1063_2.add(r1064_2).add(r10610_2).add(r10611_2).add(r10612_2);
			
	 
	r1334 = getData9("1000014,1000015,1000041",d1,d2);  r13310 = getData9("1000056, 1000057, 1000081, 1000085, 1000087, 1000101",d1,d2);
		r13312 = getData9("1000091",d1,d2);
		r1333 = r1333.add(r1334).add(r13310).add(r13312);
		
	r1244 = r1244.add(r1334);	r12410 = r12410.add(r13310); r12412 = r12412.add(r13312);
	r1243 = r1243.add(r1244).add(r12410).add(r12412);	
																						
	r13413 = getData9("1000112",d1,d2);	r1343 = r1343.add(r13413);	
																										 
		
	r14 = r14.add(r44).add(r94).add(r144).add(r244).add(r324).add(r1064).add(r1244).add(r144_2).add(r1064_2);
	r110 = r110.add(r410).add(r910).add(r1410).add(r2410).add(r3210).add(r10610).add(r12410).add(r1410_2).add(r10610_2);
	r111 = r111.add(r10611).add(r10611_2);
	r112 = r112.add(r10612).add(r10612_2);
	r113 = r113.add(r13413);
	
	r13 = r13.add(r14).add(r110).add(r111).add(r112).add(r113);
	
	
		//write to list ------------------------------------------
		/*	1	*/list.add(getLIst9(r13, r14, zero, zero, zero, zero, zero, r110, r111, r112, r113));
		/*	2	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	3	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	4	*/list.add(getLIst9(r43, r44, zero, zero, zero, zero, zero, r410, zero, zero, zero));
		
		/*	5	*/list.add(getLIst9(r53, r54, zero, zero, zero, zero, zero, r510, zero, zero, zero));
		/*	6	*/list.add(getLIst9(r63, r64, zero, zero, zero, zero, zero, r610, zero, zero, zero));
		/*	7	*/list.add(getLIst9(r73, r74, zero, zero, zero, zero, zero, r710, zero, zero, zero));
		/*	8	*/list.add(getLIst9(r83, r84, zero, zero, zero, zero, zero, r810, zero, zero, zero));
		/*	9	*/list.add(getLIst9(r93, r94, zero, zero, zero, zero, zero, r910, zero, zero, zero));
		
		/*	10	*/list.add(getLIst9(r103, r104, zero, zero, zero, zero, zero, r1010, zero, zero, zero));
		/*	11	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	12	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	13	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	14	*/list.add(getLIst9(r143, r144, zero, zero, zero, zero, zero, r1410, zero, zero, zero));
		
		/*	15	*/list.add(getLIst9(r153, r154, zero, zero, zero, zero, zero, r1510, zero, zero, zero));
		/*	16	*/list.add(getLIst9(r163, r164, zero, zero, zero, zero, zero, r1610, zero, zero, zero));
		/*	17	*/list.add(getLIst9(r173, r174, zero, zero, zero, zero, zero, r1710, zero, zero, zero));
		/*	18	*/list.add(getLIst9(r183, r184, zero, zero, zero, zero, zero, r1810, zero, zero, zero));
		/*	19	*/list.add(getLIst9(r193, r194, zero, zero, zero, zero, zero, r1910, zero, zero, zero));
		/*	20	*/list.add(getLIst9(r203, r204, zero, zero, zero, zero, zero, r2010, zero, zero, zero));
		/*	21	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	22	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	23	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	24	*/list.add(getLIst9(r243, r244, zero, zero, zero, zero, zero, r2410, zero, zero, zero));
		
		/*	25	*/list.add(getLIst9(r253, r254, zero, zero, zero, zero, zero, r2510, zero, zero, zero));
		/*	26	*/list.add(getLIst9(r263, r264, zero, zero, zero, zero, zero, r2610, zero, zero, zero));
		/*	27	*/list.add(getLIst9(r273, r274, zero, zero, zero, zero, zero, r2710, zero, zero, zero));
		/*	28	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	29	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	30	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	31	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	32	*/list.add(getLIst9(r323, r324, zero, zero, zero, zero, zero, r3210, zero, zero, zero));
		
		/*	33	*/list.add(getLIst9(r333, zero, zero, zero, zero, zero, zero, r3310, zero, zero, zero));
		/*	34	*/list.add(getLIst9(r343, r344, zero, zero, zero, zero, zero, r3410, zero, zero, zero));
		/*	35	*/list.add(getLIst9(r353, zero, zero, zero, zero, zero, zero, r3510, zero, zero, zero));
		/*	36	*/list.add(getLIst9(r363, r364, zero, zero, zero, zero, zero, r3610, zero, zero, zero));
		/*	37	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	38	*/list.add(getLIst9(r383, zero, zero, zero, zero, zero, zero, r3810, zero, zero, zero));
		/*	39	*/list.add(getLIst9(r393, r394, zero, zero, zero, zero, zero, r3910, zero, zero, zero));
		/*	40	*/list.add(getLIst9(r403, r404, zero, zero, zero, zero, zero, r4010, zero, zero, zero));
		
		/*	41	*/list.add(getLIst9(r413, r414, zero, zero, zero, zero, zero, r4110, zero, zero, zero));
		
		/*	101	*/list.add(getLIst9(r1013, r1014, zero, zero, zero, zero, zero, r10110, zero, zero, zero));		
		/*	102	*/list.add(getLIst9(r1023, r1024, zero, zero, zero, zero, zero, r10210, zero, zero, zero));
		/*	103	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	104	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	105	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		
		/*	106	*/list.add(getLIst9(r1063, r1064, zero, zero, zero, zero, zero, r10610, r10611, r10612, zero));
		/*	107	*/list.add(getLIst9(r1073, zero, zero, zero, zero, zero, zero, r10710, zero, zero, zero));
		/*	108	*/list.add(getLIst9(r1083, zero, zero, zero, zero, zero, zero, r10810, zero, zero, zero));
		/*	109	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	110	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	111	*/list.add(getLIst9(r1113, r1114, zero, zero, zero, zero, zero, r11110, zero, zero, zero));
		/*	112	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	113	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	114	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	115	*/list.add(getLIst9(r1153, zero, zero, zero, zero, zero, zero, zero, r11511, zero, zero));
		/*	116	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	117	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	118	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	119	*/list.add(getLIst9(r1193, zero, zero, zero, zero, zero, zero, zero, zero, r11912, zero));
		/*	120	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	121	*/list.add(getLIst9(r1213, zero, zero, zero, zero, zero, zero, zero, zero, r12112, zero));
		/*	122	*/list.add(getLIst9(r1223, r1224, zero, zero, zero, zero, zero, r12210, zero, zero, zero));
		/*	123	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	124	*/list.add(getLIst9(r1243, r1244, zero, zero, zero, zero, zero, r12410, zero, r12412, zero));
		
		/*	125	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	126	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	127	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	128	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	129	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	130	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	131	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	132	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		/*	133	*/list.add(getLIst9(r1333, r1334, zero, zero, zero, zero, zero, r13310, zero, r13312, zero));
		/*	134	*/list.add(getLIst9(r1343, zero, zero, zero, zero, zero, zero, zero, zero, zero, r13413));
		/*	135	*/list.add(getLIst9(zero, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero));
		
		
		return list;
		
	}
	
	protected Line  getLIst9(BigDecimal v1,BigDecimal v2,BigDecimal v3,BigDecimal v4,BigDecimal v5,BigDecimal v6,BigDecimal v7,BigDecimal v8,BigDecimal v9,BigDecimal v10,BigDecimal v11)
	{
		Line line = new Line();
		
		line.setValue1(getDiv1000(v1).toString());
		line.setValue2(getDiv1000(v2).toString());
		line.setValue3(getDiv1000(v3).toString());
		line.setValue4(getDiv1000(v4).toString());
		line.setValue5(getDiv1000(v5).toString());
		line.setValue6(getDiv1000(v6).toString());
		line.setValue7(getDiv1000(v7).toString());
		line.setValue8(getDiv1000(v8).toString());
		line.setValue9(getDiv1000(v9).toString());
		line.setValue10(getDiv1000(v10).toString());
		line.setValue11(getDiv1000(v11).toString());
		return line;
	}
	
	protected List<TablesWithC_charge_ID> initListOfTablesWithC_Charge_id(){
		List<TablesWithC_charge_ID> list = new ArrayList<TablesWithC_charge_ID>();
		
		String sql = 
				"	select c.ad_table_id, t.tablename \n" +
				"		from adempiere.ad_column c	\n" +
				"		inner join adempiere.ad_table t on t.ad_table_id = c.ad_table_id	\n" +
				"		where c.ad_element_id = 968	\n" ;
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			pstmt = DB.prepareStatement(sql, null);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				TablesWithC_charge_ID line = new TablesWithC_charge_ID();
				line.setAD_Table_ID(rs.getInt(1));
				line.setTableName(rs.getString(2));
				list.add(line);
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}

		
		return list;
	}
	
	private BigDecimal getData9(String code, Date startDate, Date endDate){
		BigDecimal result = zero;
		
		String[] words = code.split(",");  
		
//		String whereCodes = "";
//		if(words.length > 0){
//			whereCodes = " and c_charge_id in ( ";
//			for(int i=0; i < words.length; i++){
//				if(i == (words.length-1))
//					whereCodes += words[i]; 
//				else
//					whereCodes += words[i]+",";
//					
//			}
//			whereCodes += " ) \n";
//		}
		
		String whereCodes = "";
		if(words.length > 0){
			whereCodes = " and c_charge_id in ( ";
			for(int i=0; i < words.length; i++){
				if(i == (words.length-1))
					whereCodes += words[i]; 
				else
					whereCodes += words[i]+",";
					
			}
			whereCodes += " ) \n";
		}
		
		String sql = 
				  
				  "			select coalesce(sum(fa.amtacctcr + fa.amtacctdr),0)	\n"
				+ "			from fact_acct fa \n"//, c_elementvalue ce 	\n"
//				+ "			where fa.account_id = ce.c_elementvalue_id 	\n"
				;

		if(startDate != null && endDate != null){	
			sql +=	"		where fa.dateacct between '"+startDate+"'::date and '"+endDate+"'::date	\n"	;
		}else if(endDate == null){
			sql +=		"		where fa.dateacct <= '"+startDate+"'::date 	\n"	;
		}if(startDate == null){
			sql +=		"		where fa.dateacct <= '"+endDate+"'::date	\n"	;
		}
		
			sql += whereCodes;		
		
		
//		if(tablesWithC_charge_IDs == null)
//			return zero;
//		for(TablesWithC_charge_ID line: tablesWithC_charge_IDs){
				
//				String sql =
//						
//						"select coalesce(\n" +
//								"(\n" +
//								"(SELECT coalesce(sum(l.stmtAmt),0)\n" +
//								"	FROM adempiere.c_bankstatement h\n" +
//								"	inner join adempiere.c_bankstatementline l on l.c_bankstatement_id = h.c_bankstatement_id\n" +
//								"	where h.statementdate between '"+d1+"' and '"+d2+"'\n" +
//								whereCodes +
//								"and h.posted = 'Y'\n" +
//								")\n" +
//								"+\n" +
//								"(SELECT coalesce(sum(h.Payamt),0)\n" +
//								"	FROM adempiere.c_payment h\n" +
//								"	where h.dateacct between '"+d1+"' and '"+d2+"'\n" +
//								whereCodes +
//								"and h.posted = 'Y'\n" +
//								")\n" +
//								"+\n" +
//								"(SELECT coalesce(sum(l.amtacctcr + l.amtacctdr),0)\n" +
//								"	FROM adempiere.gl_journal h\n" +
//								"	inner join adempiere.gl_journalline l on l.gl_journal_id = h.gl_journal_id\n" +
//								"	where h.dateacct between '"+d1+"' and '"+d2+"'	\n" +
//								whereCodes +
//								"and h.posted = 'Y'	\n" +
//								")\n" +
//								"+ \n" +
//								"	(select coalesce(sum(l.linenetamt),0)	\n"	+ 
//								"	from adempiere.c_invoice h	\n"	+
//								"	inner join adempiere.c_invoiceline l on l.c_invoice_id = h.c_invoice_id	\n"	+
//								"	where h.dateacct between '"+d1+"' and '"+d2+"'	 	\n"	+
//								"	and h.posted = 'Y'	\n"	+
//								"	and h.c_doctype_id != '1000043'	\n"	+
//								(whereCodes.substring(0, whereCodes.indexOf("c_charge_id")) + "l." + whereCodes.substring(whereCodes.indexOf("c_charge_id"),whereCodes.length())) +
//								") \n"	+
//								
//								"+\n" +
//								"(select abs(coalesce(sum(f.amtacctcr),0))	\n" +
//								"	from adempiere.hrm_payment p	\n" +
//								"		inner join adempiere.hrm_paymentline pl on pl.hrm_payment_id = p.hrm_payment_id	\n" +
//								"		inner join adempiere.fact_acct f on f.record_id = p.hrm_payment_id	\n" +
//								"		where p.datefrom between '"+d1+"' and '"+d2+"'	\n" +
//								"			and p.posted = 'Y'	\n" +
//								"			and pl.hrm_paymentline_id = f.line_id	\n" +
//								"			and f.ad_table_id = 1000044" +
//								whereCodes +
//								")"	+		
////								" + \n"+
////								"	(select abs(coalesce(sum(f.amtacctcr),0))	\n"	+
////								"	from adempiere.m_inventory h	\n"	+
////								"	inner join adempiere.m_inventoryline l on l.m_inventory_id = h.m_inventory_id	\n"	+ 
////								"	left join adempiere.fact_acct f on f.record_id = h.m_inventory_id 	\n"	+
////								"	where h.movementdate between '"+d1+"' and '"+d2+"'	\n" +
////								"	and f.line_id = l.m_inventoryline_id 	\n"	+
////								"	and f.ad_table_id = 321	\n"	+
////								whereCodes +
////								"	)		\n"	+ 
//
//								
//								"),0)ss"						
//						;
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql, null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
//		}
//		if(result.compareTo(zero) != 0 )
//			result = result.divide(new BigDecimal(1000));
//		
//		result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
//		if(result.compareTo(zero) == 1)
//			System.out.println(result);
//				System.out.println(code);
		
		return result; 
	}	
	
	private List<Line> load10(MBCSFRInputFormCompany company) {
 		List<Line> list = new ArrayList<Line>();
		
		MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
		int C_Period_ID = period.getPeriodTo();
		MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
		Long time;

		Timestamp ts1 = p.getEndDate();//begin of choosen period 
		//ts2
		Timestamp ts2 = p.getEndDate();//choosen period 
		
		//ts1
		Date d2 = new Date(ts2.getTime());
		Calendar c = Calendar.getInstance();
		c.setTime(new Date(ts2.getTime()));
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.MONTH, 0);
		Date d1 = new Date(c.getTimeInMillis());

		String codes = "";
		
		BigDecimal r1010 = zero; 
		BigDecimal r1040 = zero;
		BigDecimal r1070 = zero;
		BigDecimal r1050 = zero;
		BigDecimal r1060 = zero;
		BigDecimal r = zero;
		
//		1000,1030,1040,1050
		
//		codes = "1010";	r1010 = getData10(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));	
//		codes =	"1040";	r1040 = getData10(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//		codes =	"1070";	r1070 = getData10(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//		codes =	"1050";	r1050 = getData10(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//		codes =	"1060";	r1060 = getData10(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));

		codes = "1010";	r1010 = getData(codes, new Timestamp(d2.getTime()), "dr","");	
		codes =	"1040";	r1040 = getData(codes,  new Timestamp(d2.getTime()), "dr","");
		codes =	"1070";	r1070 = getData(codes,  new Timestamp(d2.getTime()), "dr","");
		codes =	"1050";	r1050 = getData(codes, new Timestamp(d2.getTime()), "dr","");
		codes =	"1060";	r1060 = getData(codes, new Timestamp(d2.getTime()),	"dr","");

		
		r = r.add(r1010).add(r1040).add(r1070).add(r1050).add(r1060);
		
		Line line;
		
		/*	1010	*/	line = new Line();		line.setValue1(getDiv1000(r1010).toString()); list.add(line);
		/*	1040	*/	line = new Line();		line.setValue1(getDiv1000(r1040).toString()); list.add(line);		
		/*	1070	*/	line = new Line();		line.setValue1(getDiv1000(r1070).toString()); list.add(line);
		/*	1050	*/	line = new Line();		line.setValue1(getDiv1000(r1050).toString()); list.add(line);
		/*	1060	*/	line = new Line();		line.setValue1(getDiv1000(r1060).toString()); list.add(line);
		/*	6   	*/	line = new Line();		line.setValue1(zero.toString()); list.add(line);
		/*	total  	*/	line = new Line();		line.setValue1(getDiv1000(r).toString()); list.add(line);

		return list;
	}
		private BigDecimal getData10(String code, Timestamp startDate, Timestamp endDate) {
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			BigDecimal result = zero;
			String sql = 
					  
					  "			select coalesce(sum(fa.amtacctdr - fa.amtacctcr))	\n"
					+ "			from fact_acct fa, c_elementvalue ce 	\n"
					+ "			where fa.account_id = ce.c_elementvalue_id 	\n"
//					+ "						and fa.dateacct between '" + startDate + "' and '"+ endDate +"'	\n"
					+ "						and fa.dateacct <= '"+ endDate +"'	\n"					
					+ "						and ce.value like '"+ code+ "%'" 
					;
				

			try {
				pstmt = DB.prepareStatement(sql, null);

				rs = pstmt.executeQuery();
				while (rs.next()) {
					result = rs.getBigDecimal(1);
				}
			} catch (SQLException e) {
				s_log.log(Level.SEVERE, "Report: " + this.getClass(), e);
//				getSQlresult = e.getMessage();
				return zero;
			} finally {
				DB.close(rs, pstmt);
				rs = null;
				pstmt = null;
			}
			
			if(result == null)
				result = zero;
				
			
			if(result.compareTo(zero) != 0 )
				result = result.divide(new BigDecimal(1000));
			
			result = result.setScale(0, BigDecimal.ROUND_HALF_UP);		
			return result;
			
		}

		private List<Line> load8(MBCSFRInputFormCompany company) {
	 		List<Line> list = new ArrayList<Line>();
			
			MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
			int C_Period_ID = period.getPeriodTo();
			MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
			Long time;

			Timestamp ts1 = p.getEndDate();//begin of choosen period 
			//ts2
			Timestamp ts2 = p.getEndDate();//choosen period 
			
			//ts1
			Date d2 = new Date(ts2.getTime());
			Calendar c = Calendar.getInstance();
			c.setTime(new Date(ts2.getTime()));
			c.set(Calendar.DAY_OF_MONTH, 1);
			c.set(Calendar.MONTH, 0);
			Date d1 = new Date(c.getTimeInMillis());

			String codes = "";
			
//			BigDecimal v1 = zero; 
//			BigDecimal v2 = zero;
		
//			codes =	"601000,602000,603000";	
//			v1 = getData8(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//			
//			codes = "611000,612000,613000,614000,615000,616000,621000,622000,623000,624000,625000,626000,627000,628000, 631000,641000,642000";	
//			v2 = getData8(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//			
//			Line line;
//			/*	1010	*/	line = new Line();		line.setValue1(getDiv1000(v1).toString()); list.add(line);
//			/*	1040	*/	line = new Line();		line.setValue1(getDiv1000(v2).toString()); list.add(line);
//			BigDecimal b = zero; 
//			b = b.add(v2).add(v1);
//			/*	total	*/	line = new Line();		line.setValue1(getDiv1000(b).toString()); list.add(line);

			Line line;
			// 1.
			//osn deyatel'nost'
			codes =	"601000,602000,603000";	
			List<iline> lines = getData8(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
			BigDecimal v1 = zero;
			for(int i=0; i < lines.size(); i++){
				v1 = v1.add(lines.get(i).getF1());
			}
			
			//header
			line = new Line();	line.setValue3("1."); line.setValue2("Доходы по основным видам деятельности всего"); line.setValue1(v1.toString()); list.add(line);
			//others
			for(int i=0; i < lines.size(); i++){
				line = new Line();	line.setValue2(lines.get(i).getCode()); line.setValue1(lines.get(i).getF1().toString()); list.add(line);
			}
			
			
			// 2.
			//neosn deyatel'nost'
			codes = "611000,612000,613000,614000,615000,616000,621000,622000,623000,624000,625000,626000,627000,628000, 631000,641000,642000";	
			lines = getData8(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
			BigDecimal v2 = zero;
			for(int i=0; i < lines.size(); i++){
				v2 = v2.add(lines.get(i).getF1());
			}

			//header
			line = new Line();	line.setValue3("2."); line.setValue2("Доходы от неосновной деятельности всего"); line.setValue1(v2.toString()); list.add(line);
			//others
			for(int i=0; i < lines.size(); i++){
				line = new Line();	line.setValue2(lines.get(i).getCode()); line.setValue1(lines.get(i).getF1().toString()); list.add(line);
			}
			
			BigDecimal summ = v1.add(v2);
			line = new Line();	line.setValue2("ИТОГО ДОХОДОВ"); line.setValue1(summ.toString()); list.add(line);
			
			return list;
		}
		
		
		private List<iline> getData8(String codes, Date dateFrom, Date dateTo){
			List<iline> lines = new ArrayList<iline>();
			BigDecimal result = zero;
			StringBuffer sql = new StringBuffer();
			
			String[] words = codes.split(",");  
			
			String whereCodes = "";
			if(words.length > 0){
				whereCodes = " and ( ";
				for(int i=0; i < words.length; i++){
					if(i == (words.length-1))
						whereCodes += "	ce.value like '"+words[i]+"%'	"; 
					else
						whereCodes += "	ce.value like '"+words[i]+"%' or ";
						
				}
				whereCodes += " ) ";
					
			}

//			//prepare sql
			if(whereCodes.equals(""))
				return lines;

			sql.append(
					"	select ce.name, coalesce(sum(fa.amtacctcr),0)	\n" +
					"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n"	+ 
					"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	+	
					"		" + whereCodes + "\n" +
					"		group by  ce.name"
					);
			
			
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			
			try {
				pstmt = DB.prepareStatement(sql.toString(), null);
				rs = pstmt.executeQuery();
				while (rs.next()) {
						result = rs.getBigDecimal(2);
						if(result.compareTo(zero) != 0 )
							result = result.divide(new BigDecimal(1000));
						
						result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
						
					iline l = new iline();
					l.setCode(rs.getString(1));
					l.setF1(result);
					lines.add(l);
				}
			} catch (SQLException e) {
				s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
			} finally {
				DB.close(rs, pstmt);
				rs = null;
				pstmt = null;
			}
			
//			if(result.compareTo(zero) != 0 )
//				result = result.divide(new BigDecimal(1000));
//			
//			result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
			
			return lines; 
		}	

		
		private List<Line> load13(MBCSFRInputFormCompany company) {
	 		List<Line> list = new ArrayList<Line>();
			
			MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
			int C_Period_ID = period.getPeriodTo();
			MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
			Long time;

			Timestamp ts1 = p.getEndDate();//begin of choosen period 
			//ts2
			Timestamp ts2 = p.getEndDate();//choosen period 
			
			//ts1
			Date d2 = new Date(ts2.getTime());
			Calendar c = Calendar.getInstance();
			c.setTime(new Date(ts2.getTime()));
			c.set(Calendar.DAY_OF_MONTH, 1);
			c.set(Calendar.MONTH, 0);
			Date d1 = new Date(c.getTimeInMillis());

			String codes = "";
			
			BigDecimal r131000restBegin = zero; BigDecimal r131000fromSupplier = zero; BigDecimal r131000writeOff = zero; BigDecimal r131000restEnd= zero;  
			BigDecimal r132000restBegin = zero; BigDecimal r132000fromSupplier = zero; BigDecimal r132000writeOff = zero; BigDecimal r132000restEnd= zero;			
			BigDecimal r133000restBegin = zero; BigDecimal r133000fromSupplier = zero; BigDecimal r133000writeOff = zero; BigDecimal r133000restEnd= zero;
			BigDecimal r1340restBegin = zero; BigDecimal r1340fromSupplier = zero; BigDecimal r1340writeOff = zero; BigDecimal r1340restEnd= zero;			
			BigDecimal r1350restBegin = zero; BigDecimal r1350fromSupplier = zero; BigDecimal r1350writeOff = zero; BigDecimal r1350restEnd= zero;
			BigDecimal r1360restBegin = zero; BigDecimal r1360fromSupplier = zero; BigDecimal r1360writeOff = zero; BigDecimal r1360restEnd= zero;			
			BigDecimal rTotalRestBegin = zero; BigDecimal rTotalfromSupplier = zero; BigDecimal rTotalWriteOff = zero; BigDecimal rTotalRestEnd= zero;
			
			
			codes = "131000";	
			r131000restBegin = getData13(codes, new Timestamp(d1.getTime()), null, "");
			r131000fromSupplier = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "dr");
			r131000writeOff = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "cr");
			r131000restEnd = getData13(codes, null, new Timestamp(d2.getTime()), "");
			
			codes = "132000";	
			r132000restBegin = getData13(codes, new Timestamp(d1.getTime()), null, "");
			r132000fromSupplier = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "dr");
			r132000writeOff = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "cr");
			r132000restEnd = getData13(codes, null, new Timestamp(d2.getTime()), "");			
			
			codes = "133000";	
			r133000restBegin = getData13(codes, new Timestamp(d1.getTime()), null, "");
			r133000fromSupplier = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "dr");
			r133000writeOff = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "cr");
			r133000restEnd = getData13(codes, null, new Timestamp(d2.getTime()), "");			

			codes = "1340";	
			r1340restBegin = getData13(codes, new Timestamp(d1.getTime()), null, "");
			r1340fromSupplier = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "dr");
			r1340writeOff = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "cr");
			r1340restEnd = getData13(codes, null, new Timestamp(d2.getTime()), "");			

			codes = "1350";	
			r1350restBegin = getData13(codes, new Timestamp(d1.getTime()), null, "");
			r1350fromSupplier = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "dr");
			r1350writeOff = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "cr");
			r1350restEnd = getData13(codes, null, new Timestamp(d2.getTime()), "");			

			codes = "1360";	
			r1360restBegin = getData13(codes, new Timestamp(d1.getTime()), null, "");
			r1360fromSupplier = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "dr");
			r1360writeOff = getData13(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "cr");
			r1360restEnd = getData13(codes, null, new Timestamp(d2.getTime()), "");			
			
			rTotalRestBegin = rTotalRestBegin.add(r131000restBegin).add(r132000restBegin).add(r133000restBegin).add(r1340restBegin).add(r1350restBegin).add(r1360restBegin);
			rTotalfromSupplier = rTotalfromSupplier.add(r131000fromSupplier).add(r132000fromSupplier).add(r133000fromSupplier).add(r1340fromSupplier).add(r1350fromSupplier).add(r1360fromSupplier);
			rTotalWriteOff = rTotalWriteOff.add(r131000writeOff).add(r132000writeOff).add(r133000writeOff).add(r1340writeOff).add(r1350writeOff).add(r1360writeOff);
			rTotalRestEnd = rTotalRestEnd.add(r131000restEnd).add(r132000restEnd).add(r133000restEnd).add(r1340restEnd).add(r1350restEnd).add(r1360restEnd);
			
			Line line;
			
			/*	restBegin	*/	
			line = new Line();		
				line.setValue1(r131000restBegin.toString()); 
				line.setValue2(r132000restBegin.toString());	
				line.setValue3(r133000restBegin.toString());
				line.setValue4(r1340restBegin.toString());
				line.setValue5(r1350restBegin.toString());
				line.setValue6(r1360restBegin.toString());
				line.setValue7(rTotalRestBegin.toString());
			list.add(line);
			/*	поступления, в том числе	*/
			line = new Line();		
				line.setValue1(r131000fromSupplier.toString()); 
				line.setValue2(r132000fromSupplier.toString());	
				line.setValue3(r133000fromSupplier.toString());
				line.setValue4(r1340fromSupplier.toString());
				line.setValue5(r1350fromSupplier.toString());
				line.setValue6(r1360fromSupplier.toString());
				line.setValue7(rTotalfromSupplier.toString());
			list.add(line);			
			/*	fromSupplier	*/
			line = new Line();		
				line.setValue1(r131000fromSupplier.toString()); 
				line.setValue2(r132000fromSupplier.toString());	
				line.setValue3(r133000fromSupplier.toString());
				line.setValue4(r1340fromSupplier.toString());
				line.setValue5(r1350fromSupplier.toString());
				line.setValue6(r1360fromSupplier.toString());
				line.setValue7(rTotalfromSupplier.toString());
			list.add(line);			
			/*	прочее	*/
			line = new Line();		
				line.setValue1(zero.toString()); 
				line.setValue2(zero.toString());	
				line.setValue3(zero.toString());
				line.setValue4(zero.toString());
				line.setValue5(zero.toString());
				line.setValue6(zero.toString());
				line.setValue7(zero.toString());
			list.add(line);			
			/*	внутренее перемещение	*/
			line = new Line();		
				line.setValue1(zero.toString()); 
				line.setValue2(zero.toString());	
				line.setValue3(zero.toString());
				line.setValue4(zero.toString());
				line.setValue5(zero.toString());
				line.setValue6(zero.toString());
				line.setValue7(zero.toString());
			list.add(line);			
			
			/*	Расход, в том числе	*/
			line = new Line();		
				line.setValue1(r131000writeOff.toString()); 
				line.setValue2(r132000writeOff.toString());	
				line.setValue3(r133000writeOff.toString());
				line.setValue4(r1340writeOff.toString());
				line.setValue5(r1350writeOff.toString());
				line.setValue6(r1360writeOff.toString());
				line.setValue7(rTotalWriteOff.toString());
			list.add(line);						
			/*	реализовано покупателям	*/
			line = new Line();		
				line.setValue1(zero.toString()); 
				line.setValue2(zero.toString());	
				line.setValue3(zero.toString());
				line.setValue4(zero.toString());
				line.setValue5(zero.toString());
				line.setValue6(zero.toString());
				line.setValue7(zero.toString());
			list.add(line);
			/*	writeOff	*/
			line = new Line();		
				line.setValue1(r131000writeOff.toString()); 
				line.setValue2(r132000writeOff.toString());	
				line.setValue3(r133000writeOff.toString());
				line.setValue4(r1340writeOff.toString());
				line.setValue5(r1350writeOff.toString());
				line.setValue6(r1360writeOff.toString());
				line.setValue7(rTotalWriteOff.toString());
			list.add(line);				
			/*	прочее	*/
			line = new Line();		
				line.setValue1(zero.toString()); 
				line.setValue2(zero.toString());	
				line.setValue3(zero.toString());
				line.setValue4(zero.toString());
				line.setValue5(zero.toString());
				line.setValue6(zero.toString());
				line.setValue7(zero.toString());
			list.add(line);
			/*	внутренее перемещение	*/
			line = new Line();		
				line.setValue1(zero.toString()); 
				line.setValue2(zero.toString());	
				line.setValue3(zero.toString());
				line.setValue4(zero.toString());
				line.setValue5(zero.toString());
				line.setValue6(zero.toString());
				line.setValue7(zero.toString());
			list.add(line);			
			/*	restEnd	*/
			line = new Line();		
				line.setValue1(r131000restEnd.toString()); 
				line.setValue2(r132000restEnd.toString());	
				line.setValue3(r133000restEnd.toString());
				line.setValue4(r1340restEnd.toString());
				line.setValue5(r1350restEnd.toString());
				line.setValue6(r1360restEnd.toString());
				line.setValue7(rTotalRestEnd.toString());
			list.add(line);									

			return list;
		}
			private BigDecimal getData13(String code, Timestamp startDate, Timestamp endDate, String type) {
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				BigDecimal result = zero;
				String sql = "";
						if(type.equals(""))
							sql += "			select coalesce(sum(fa.amtacctdr - fa.amtacctcr))	\n";
						else if(type.equals("dr"))
							sql += "			select coalesce(sum(fa.amtacctdr))	\n";
						else if(type.equals("cr"))
							sql += "			select coalesce(sum(fa.amtacctcr))	\n";							
						
						sql +=  "			from fact_acct fa, c_elementvalue ce 	\n"
						+ "			where fa.account_id = ce.c_elementvalue_id 	\n";
//						+ "						and fa.dateacct between '" + startDate + "' and '"+ endDate +"'	\n"
						if(endDate == null)
							sql	+=	"						and fa.dateacct <= '"+ startDate +"'	\n";
						else if(startDate == null)
							sql	+=	"						and fa.dateacct <= '"+ endDate +"'	\n";
						else
							sql	+=	"						and fa.dateacct between '"+ startDate +"' and '"+ endDate +"'	\n";
						
					sql +=	"						and ce.value like '"+ code+ "%'"; 
					

				try {
					pstmt = DB.prepareStatement(sql, null);

					rs = pstmt.executeQuery();
					while (rs.next()) {
						result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "Report: " + this.getClass(), e);
					return zero;
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
				if(result == null)
					result = zero;
				
				if(result.compareTo(zero) != 0 )
					result = result.divide(new BigDecimal(1000));
				
				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);		
				return result;
				
			}
		
			private List<Line> load11(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
				
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodTo();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
				Long time;

				Timestamp ts1 = p.getEndDate();//begin of choosen period 
				//ts2
				Timestamp ts2 = p.getEndDate();//choosen period 
				
				//ts1
				Date d2 = new Date(ts2.getTime());
				Calendar c = Calendar.getInstance();
				c.setTime(new Date(ts2.getTime()));
				c.set(Calendar.DAY_OF_MONTH, 1);
				c.set(Calendar.MONTH, 0);
				Date d1 = new Date(c.getTimeInMillis());

				BigDecimal v1 = zero;   
				BigDecimal v2 = zero; 			
				BigDecimal v3 = zero;   
				
//				String codes = "121000,122000,123000,124000, 1250*, 126000, 128001, 128002,128003,128004";				
//				v1 = getData11(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), "");
				String codes = "";
				
		 		codes = "1200,1250,1280,127000,127000_16";//.clear(); codes.add(1200);codes.add(1250);codes.add(1280);//codes.add(127000);codes.add(127000_16);
				v1 = getData(codes, new Timestamp(d2.getTime()), "dr","");
				
				codes = "129000";
				v2 = getData(codes, new Timestamp(d2.getTime()), "dr","");
				
				Line line;
				
				/*	restBegin	*/	
				line = new Line();	line.setValue1(getDiv1000(v1).toString());	list.add(line);
				line = new Line();	line.setValue1(getDiv1000(v2).toString());	list.add(line);
				line = new Line();	line.setValue1(getDiv1000(v1).toString());	list.add(line);

				return list;
			}

			private BigDecimal getData11(String codes, Date dateFrom, Date dateTo, String type){
				BigDecimal result = zero;
				StringBuffer sql = new StringBuffer();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
						else
							whereCodes += "	ce.value like '"+words[i]+"%' or ";
							
					}
					whereCodes += " ) ";
						
				}

//				//prepare sql
				if(whereCodes.equals(""))
					return result;
				
//				if(type.equals("dr"))
//					if(codes.equals("50"))
						sql.append("	select coalesce(sum(fa.amtacctcr - fa.amtacctdr),0)	\n");
//					else	
//						sql.append("	select coalesce(sum(fa.amtacctdr),0)	\n");
//				else
//					sql.append("	select coalesce(sum(fa.amtacctcr),0)	\n");
					
				sql.append(
						"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n"	+ 
						"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	+	
						"		" + whereCodes
						);
				
//				if(codes.equals("541000") || codes.equals("543000"))
//					sql.append(
//					 "				group by fa.dateacct " 			
//					+ "				order by fa.dateacct " 
//					+ " 			OFFSET 0 FETCH FIRST 1 ROWS ONLY		" 
//				);
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
				if(result.compareTo(zero) != 0 )
					result = result.divide(new BigDecimal(1000));
				
				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
				
				return result; 
			}

			private List<Line> load12(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
		 		List<Line> list2 = new ArrayList<Line>();
		 		
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodTo();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
				Long time;

				Timestamp ts1 = p.getEndDate();//begin of choosen period 
				//ts2
				Timestamp ts2 = p.getEndDate();//choosen period 
				
				//ts1
				Date d2 = new Date(ts2.getTime());
				Calendar c = Calendar.getInstance();
				c.setTime(new Date(ts2.getTime()));
				c.set(Calendar.DAY_OF_MONTH, 1);
				c.set(Calendar.MONTH, 0);
				Date d1 = new Date(c.getTimeInMillis());

				String codes ="1200,1280,1250";
				list = getData12(new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), codes);

				BigDecimal summ = Env.ZERO;
				BigDecimal summ129 = Env.ZERO;
				
				// get total summ
				for(Line l: list){
					String sum  = l.getValue2();
					String sum129  = l.getValue3();
					try{
						BigDecimal t = new BigDecimal(sum);
						summ = summ.add(t);
						
						BigDecimal t129 = new BigDecimal(sum129);
						summ129 = summ129.add(t129);
						
					}catch(Exception e){}
				}
				

				Line line = null;
				
				int rows = list.size();
				if(rows > 0){
					for(int i = 0; i < rows; i++){
						BigDecimal b = new BigDecimal(list.get(i).getValue2());
						
						if(b == null || b.compareTo(zero) == 0)
							continue;
						
						line  = new Line();
						line = new Line(); 
//							line.setValue1(list.get(i).getValue1());
							line.setValue2(list.get(i).getValue1());
//							line.setValue3(list.get(i).getValue2());
//							line.setValue4(list.get(i).getValue3());
//							line.setValue5(getDiv1000(new BigDecimal(list.get(i).getValue4())).toString());
							line.setValue5(getDiv1000(new BigDecimal(list.get(i).getValue2())).toString());
//							line.setValue5(
//							line.setValue6(list.get(i).getValue5());
							line.setValue13(list.get(i).getValue3());
						list2.add(line);
					}
				}
				
				line = new Line(); line.setValue5(getDiv1000(summ).toString()); line.setValue13(getDiv1000(summ129).toString()); list2.add(line);				
				
				return list2;
			}

			
			private List<Line> getData12(Date dateFrom, Date dateTo, String codes){
				StringBuffer sql = new StringBuffer();
				List<Line> result = new ArrayList<Line>();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
						else
							whereCodes += "	ce.value like '"+words[i]+"%' or ";
							
					}
					whereCodes += " ) ";
				}
				
				sql.append(
						"	select	\n"	+ 
//						"		b.name, fa.dateacct, fa.description, coalesce(fa.amtacctcr - fa.amtacctdr) summ	\n"	+
						"		b.name, sum(coalesce(fa.amtacctcr - fa.amtacctdr)) summ	\n"	+								
						"		,(case when ce.value like '1290%' then coalesce(fa.amtacctcr - fa.amtacctdr) else 0 end) a129	\n"	+
						"		, b.c_bpartner_id	\n" +		
						"	from 	adempiere.fact_acct fa	\n"	+
						"	inner join adempiere.c_elementvalue ce 	on ce.c_elementvalue_id  = fa.account_id	\n"	+
						"	left join adempiere.c_bpartner b on b.c_bpartner_id = fa.c_bpartner_id	\n"	+

						"	where fa.dateacct <= '"+	dateTo	+"'	\n"	
						+ whereCodes +
						"	group by b.name, a129, b.c_bpartner_id	\n" +  
						"	order by b.name	\n"	
						);
				
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				int i = 1;
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
						Line line = new Line();
						line.setValue1(rs.getString(1));
//						line.setValue2(rs.getDate(2).toString());
//						line.setValue3(rs.getString(2));
						
						BigDecimal t = Env.ZERO;
						t = getData(codes, new Timestamp(dateTo.getTime()), "dr", String.valueOf(rs.getInt(4)));
//						t = rs.getBigDecimal(2);
//						if(t.compareTo(zero) < 0 )
//							t = t.abs();//divide(new BigDecimal(1000));
//						
//						t = t.setScale(0, BigDecimal.ROUND_HALF_UP);
						
						
						line.setValue2(t.toString());
						line.setValue3(rs.getBigDecimal(3).toString());
						
//						String v1 = rs.getBigDecimal(2).toString();
//						String v2 = getData(codes, new Timestamp(dateTo.getTime()), "dr", String.valueOf(rs.getInt(4))).toString();
//								
//						System.out.println("v1 = " + v1 + ",	v2 = " + v2);
//						line.setValue2(v2);
						result.add(line);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
				
				return result; 
			}				
			
			private List<Line> load14(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
				
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodFrom();
				int C_Period_ID2 = period.getPeriodTo();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
				MPeriod p2 = new MPeriod(Env.getCtx(), C_Period_ID2, null);
				Long time;

				Timestamp ts1 = p.getStartDate();//begin of choosen period 
				//ts2
				Timestamp ts2 = p2.getEndDate();//choosen period 
				
				//ts1
				Date d2 = new Date(ts2.getTime());
//				Calendar c = Calendar.getInstance();
//				c.setTime(new Date(ts2.getTime()));
//				c.set(Calendar.DAY_OF_MONTH, 1);
//				c.set(Calendar.MONTH, 0);
				Date d1 = new Date(ts1.getTime());

				String codes = "";
				
				BigDecimal r12 = zero;	BigDecimal r13 = zero;	BigDecimal r14 = zero;	BigDecimal r15 = zero;	BigDecimal r16 = zero;	BigDecimal r17 = zero;
										BigDecimal r23 = zero;	BigDecimal r24 = zero;	BigDecimal r25 = zero; 							BigDecimal r27 = zero;
				BigDecimal r32 = zero;	BigDecimal r33 = zero;	BigDecimal r34 = zero; 	BigDecimal r35 = zero;	BigDecimal r36 = zero;	BigDecimal r37 = zero;
				
										BigDecimal r63 = zero;	BigDecimal r64 = zero; BigDecimal r65 = zero;							BigDecimal r67 = zero;
										BigDecimal r73 = zero;	BigDecimal r74 = zero; BigDecimal r75 = zero;							BigDecimal r77 = zero;
										BigDecimal r83 = zero;	BigDecimal r84 = zero; BigDecimal r85 = zero;							BigDecimal r87 = zero;				
										BigDecimal r93 = zero;	BigDecimal r94 = zero; BigDecimal r95 = zero;							BigDecimal r97 = zero;				
			
				BigDecimal r232 = zero;	BigDecimal r233 = zero;	BigDecimal r234 = zero; BigDecimal r235 = zero;	BigDecimal r236 = zero;	BigDecimal r237 = zero;

				BigDecimal r2 = zero;	BigDecimal r3 = zero;	BigDecimal r4 = zero; BigDecimal r5 = zero;	BigDecimal r6 = zero;	BigDecimal r7 = zero;
				
				/*	1	*/
				codes =	"141000";	r12 = getData14(codes, new Timestamp(d1.getTime()), null,"saldo");
				codes =	"311000";	r13 = getData14(codes, new Timestamp(d1.getTime()), null,"saldo");
				codes =	"311000,141000";	r14 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"cr");
				codes =	"311000,141000";	r15 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"dr");
				codes =	"141000";	r16 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");
				codes =	"311000";	r17 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");
				
				
				/*	2	*/
				codes =	"312000";	r23 = getData14(codes, new Timestamp(d1.getTime()), null, "saldo");
				codes =	"312000";	r24 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"cr");				
				codes =	"312000";	r25 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"dr");
				codes =	"312000";	r27 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");

				/*	3	*/
				codes =	"142000";	r32 = getData14(codes, new Timestamp(d1.getTime()), null,"saldo");
				codes =	"313000";	r33 = getData14(codes, new Timestamp(d1.getTime()), null,"saldo");
				codes =	"313000,142000";	r34 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"cr");				
				codes =	"313000,142000";	r35 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"dr");
				codes =	"142000";	r36 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");
				codes =	"313000";	r37 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");

				
				/*	6	*/
				codes =	"315000";	r63 = getData14(codes, new Timestamp(d1.getTime()), null, "saldo");
				codes =	"315000";	r64 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"cr");				
				codes =	"315000";	r65 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"dr");
				codes =	"315000";	r67 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");

				/*	7	*/
				codes =	"318000";	r73 = getData14(codes, new Timestamp(d1.getTime()), null, "saldo");
				codes =	"318000";	r74 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"cr");				
				codes =	"318000";	r75 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"dr");
				codes =	"318000";	r77 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");
				
				/*	8	*/
				codes =	"316000";	r83 = getData14(codes, new Timestamp(d1.getTime()), null, "saldo");
				codes =	"316000";	r84 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"cr");				
				codes =	"316000";	r85 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"dr");
				codes =	"316000";	r87 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");
				
				/*	9	*/
				codes =	"317000";	r93 = getData14(codes, new Timestamp(d1.getTime()), null, "saldo");
				codes =	"317000";	r94 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"cr");				
				codes =	"317000";	r95 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"dr");
				codes =	"317000";	r97 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");
				
//				/*	8	*/
//				codes =	"317000";	r83 = getData14(codes, null, new Timestamp(d1.getTime()),0);
//				//r27 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//				codes =	"317000";	r87 = getData14(codes, null, new Timestamp(d2.getTime()),0);

				
//				/*	9	*/
//				codes =	"318000";	r93 = getData14(codes, null, new Timestamp(d1.getTime()),0);
//				//r27 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//				codes =	"318000";	r97 = getData14(codes, null, new Timestamp(d2.getTime()),0);				

				/*	23	*/
				codes =	"143000";	r232 = getData14(codes, new Timestamp(d1.getTime()), null,"saldo");
				codes =	"319000";	r233 = getData14(codes, new Timestamp(d1.getTime()), null,"saldo");
				codes =	"319000,143000";	r234 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"cr");
				codes =	"319000,143000";	r235 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"dr");
				codes =	"143000";	r236 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");
				codes =	"319000";	r237 = getData14(codes, null, new Timestamp(d2.getTime()),"saldo-");

				/*	23	*/
//				codes =	"143000";	r233 = getData14(codes, null, new Timestamp(d1.getTime()),0);
//				codes =	"319000";	r234 = getData14(codes, null, new Timestamp(d1.getTime()),0);				
//				//r27 = getData14(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//				codes =	"143000";	r236 = getData14(codes, null, new Timestamp(d1.getTime()),0);				
//				codes =	"319000";	r237 = getData14(codes, null, new Timestamp(d2.getTime()),0);				
				
				r2 = r2.add(r12).add(r32).add(r232);
				r3 = r3.add(r13).add(r23).add(r33).add(r63).add(r83).add(r93).add(r233);
				
				r4 = r4.add(r14).add(r24).add(r34).add(r64).add(r84).add(r94).add(r234);
				r5 = r5.add(r15).add(r25).add(r35).add(r65).add(r85).add(r95).add(r235);
				r6 = r6.add(r16).add(r36).add(r236);
				r7 = r7.add(r17).add(r27).add(r37).add(r67).add(r87).add(r97).add(r237);
				
				Line line;
				
				/*	1	*/	
				line = new Line();		
					line.setValue1(getDiv1000(r12).toString()); line.setValue2(getDiv1000(r13).toString()); line.setValue3(getDiv1000(r14).toString()); 
					line.setValue4(getDiv1000(r15).toString()); line.setValue5(getDiv1000(r16).toString()); line.setValue6(getDiv1000(r17).toString());
				list.add(line);
						
				/*	2	*/	
				line = new Line();		
					line.setValue1(zero.toString());line.setValue2(getDiv1000(r23).toString()); line.setValue3(getDiv1000(r24).toString()); 
					line.setValue4(getDiv1000(r25).toString()); line.setValue5(zero.toString());line.setValue6(getDiv1000(r27).toString());
				list.add(line);

				/*	3	*/	
				line = new Line();		
					line.setValue1(getDiv1000(r32).toString()); line.setValue2(getDiv1000(r33).toString()); line.setValue3(getDiv1000(r34).toString()); 
					line.setValue4(getDiv1000(r35).toString()); line.setValue5(getDiv1000(r36).toString()); line.setValue6(getDiv1000(r37).toString());
				list.add(line);				

				/*	4	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);				
				
				/*	5	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);				
				
				/*	6	*/	
				line = new Line();		
					line.setValue1(zero.toString());line.setValue2(getDiv1000(r63).toString()); line.setValue3(getDiv1000(r64).toString()); 
					line.setValue4(getDiv1000(r65).toString()); line.setValue5(zero.toString());line.setValue6(getDiv1000(r67).toString());
				list.add(line);
				
				/*	7	*/	
				line = new Line();		
				line.setValue1(zero.toString());line.setValue2(getDiv1000(r73).toString()); line.setValue3(getDiv1000(r74).toString()); 
					line.setValue4(getDiv1000(r75).toString()); line.setValue5(zero.toString());line.setValue6(getDiv1000(r77).toString());
				list.add(line);			
				
				/*	8	*/	
				line = new Line();		
				line.setValue1(zero.toString());line.setValue2(getDiv1000(r83).toString()); line.setValue3(getDiv1000(r84).toString()); 
					line.setValue4(getDiv1000(r85).toString()); line.setValue5(zero.toString());line.setValue6(getDiv1000(r87).toString());
				list.add(line);				
				
				/*	9	*/	
				line = new Line();		
				line.setValue1(zero.toString());line.setValue2(getDiv1000(r93).toString()); line.setValue3(getDiv1000(r94).toString()); 
					line.setValue4(getDiv1000(r95).toString()); line.setValue5(zero.toString());line.setValue6(getDiv1000(r97).toString());
				list.add(line);				

				/*	10	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);
				
				/*	11	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);				
				
				/*	12	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);				
				
				/*	13	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);				
				
				/*	14	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);				
				
				/*	15	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);
				
				/*	16	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);
				
				/*	17	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);
				
				/*	18	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);
				
				/*	19	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);
				
				/*	20	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);
				
				/*	21	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);
				
				/*	22	*/	
				line = new Line();		
					line.setValue1(zero.toString()); line.setValue2(zero.toString()); line.setValue3(zero.toString()); 
					line.setValue4(zero.toString()); line.setValue5(zero.toString()); line.setValue6(zero.toString());
				list.add(line);				
				
				/*	23	*/	
				line = new Line();		
				line.setValue1(zero.toString());line.setValue2(getDiv1000(r233).toString()); line.setValue3(getDiv1000(r234).toString()); 
					line.setValue4(getDiv1000(r235).toString());line.setValue5(zero.toString());line.setValue6(getDiv1000(r237).toString());
				list.add(line);				

				/*	24	*/	
				line = new Line();		
				line.setValue1(getDiv1000(r2).toString());line.setValue2(getDiv1000(r3).toString()); line.setValue3(getDiv1000(r4).toString()); 
					line.setValue4(getDiv1000(r5).toString());line.setValue5(getDiv1000(r6).toString());line.setValue6(getDiv1000(r7).toString());
				list.add(line);
				
				return list;
			}

			
			private BigDecimal getData14(String codes, Date dateFrom, Date dateTo, String type){
				BigDecimal result = zero;
				StringBuffer sql = new StringBuffer();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
						else
							whereCodes += "	ce.value like '"+words[i]+"%' or ";
							
					}
					whereCodes += " ) ";
						
				}

//				//prepare sql
				if(whereCodes.equals(""))
					return result;

				if(type.equals("cr"))
					sql.append(	"	select coalesce(sum(fa.amtacctcr),0)	\n" );
				else if(type.equals("saldo") )
					sql.append(	"	select coalesce(sum(fa.amtacctcr - fa.amtacctdr),0)	\n" );
				else if(type.equals("saldo-"))
					sql.append(	"	select coalesce(sum(fa.amtacctdr - fa.amtacctcr),0)	\n" );
				
				else if(type.equals("dr"))
					sql.append(	"	select coalesce(sum(fa.amtacctdr),0)	\n" );
				
				sql.append(
						"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n" 
						);
				
				
				if(dateFrom != null && dateTo != null){	
					sql.append(	"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	);
				}else if(dateTo == null){
					sql.append(	"		and fa.dateacct < '"+dateFrom+"'::date 	\n"	);
				}if(dateFrom == null){
					sql.append(	"		and fa.dateacct <= '"+dateTo+"'::date	\n"	);
				}
				
					sql.append(whereCodes);				
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
//				if(result.compareTo(zero) == -1 )
					result = result.abs();//multiply(new BigDecimal(-1);
//				
//				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
				
				return result; 
			}	
			
			
			private List<Line> load15(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
				
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodTo();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
				Long time;

				Timestamp ts1 = p.getEndDate();//begin of choosen period 
				//ts2
				Timestamp ts2 = p.getEndDate();//choosen period 
				
				//ts1
				Date d2 = new Date(ts2.getTime());
				Calendar c = Calendar.getInstance();
				c.setTime(new Date(ts2.getTime()));
				c.set(Calendar.DAY_OF_MONTH, 1);
				c.set(Calendar.MONTH, 0);
				Date d1 = new Date(c.getTimeInMillis());

				String codes = "";
				
				BigDecimal r161000 = zero; 
				BigDecimal r162000 = zero;
				BigDecimal r163000 = zero;
				BigDecimal r14 = zero;
				BigDecimal rTotal = zero;
			
//				codes =	"161000";	
//				r161000 = getData15(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//				
//				codes = "162000";	
//				r162000 = getData15(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//
//				codes = "163000";	
//				r163000 = getData15(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//				
//				codes = "142000,143000";	
//				r14 = getData15(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
				
//				331000,332000,333000,334000,336000,337000,338000,3390
				
				codes =	"161000";	
				r161000 = getData(codes, new Timestamp(d2.getTime()), "dr","");
				
				codes = "162000";	
				r162000 = getData(codes, new Timestamp(d2.getTime()), "dr","");

				codes = "163000";	
				r163000 = getData(codes, new Timestamp(d2.getTime()), "dr","");
				
				codes = "142000,143000";	
				r14 = getData(codes, new Timestamp(d2.getTime()), "dr","");				
				rTotal = rTotal.add(r161000).add(r162000).add(r163000).add(r14);
				
				Line line;
				/*	1	*/	line = new Line();		line.setValue1(getDiv1000(r161000).toString()); list.add(line);
				/*	2	*/	line = new Line();		line.setValue1(getDiv1000(r162000).toString()); list.add(line);		
				/*	3	*/	line = new Line();		line.setValue1(getDiv1000(r163000).toString()); list.add(line);
				/*	4	*/	line = new Line();		line.setValue1(getDiv1000(r14).toString()); list.add(line);
				/*	5	*/	line = new Line();		line.setValue1(getDiv1000(rTotal).toString()); list.add(line);
				
				return list;
			}

			
			private BigDecimal getData15(String codes, Date dateFrom, Date dateTo){
				BigDecimal result = zero;
				StringBuffer sql = new StringBuffer();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
						else
							whereCodes += "	ce.value like '"+words[i]+"%' or ";
							
					}
					whereCodes += " ) ";
						
				}

//				//prepare sql
				if(whereCodes.equals(""))
					return result;

				sql.append(
						"	select coalesce(sum(fa.amtacctdr - fa.amtacctcr),0)	\n" +
						"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n"	+ 
						"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	+	
						"		" + whereCodes
						);
				
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
				if(result.compareTo(zero) != 0 )
					result = result.divide(new BigDecimal(1000));
				
				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
				
				return result; 
			}	

			private List<Line> load16(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
				
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodFrom();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);

				int C_Period_ID2 = period.getPeriodTo();
				MPeriod p2 = new MPeriod(Env.getCtx(), C_Period_ID2, null);

				Date d1 = p.getStartDate();
				Date d2 = p2.getEndDate();
				
				String codes = "";
				
				BigDecimal r11 = zero; BigDecimal r12 = zero; BigDecimal r13 = zero;	BigDecimal r14 = zero;	BigDecimal r15 = zero; BigDecimal r16 = zero;
				BigDecimal r21 = zero; BigDecimal r22 = zero; BigDecimal r23 = zero;	BigDecimal r24 = zero;	BigDecimal r25 = zero; BigDecimal r26 = zero;
				BigDecimal r31 = zero; BigDecimal r32 = zero; BigDecimal r33 = zero;	BigDecimal r34 = zero;	BigDecimal r35 = zero; BigDecimal r36 = zero;
				BigDecimal r41 = zero; BigDecimal r42 = zero; BigDecimal r43 = zero;	BigDecimal r44 = zero;	BigDecimal r45 = zero; BigDecimal r46 = zero;
				BigDecimal r51 = zero; BigDecimal r52 = zero; BigDecimal r53 = zero;	BigDecimal r54 = zero;	BigDecimal r55 = zero; BigDecimal r56 = zero;
				BigDecimal r61 = zero; BigDecimal r62 = zero; BigDecimal r63 = zero;	BigDecimal r64 = zero;	BigDecimal r65 = zero; BigDecimal r66 = zero;
				BigDecimal r71 = zero; BigDecimal r72 = zero; BigDecimal r73 = zero;	BigDecimal r74 = zero;	BigDecimal r75 = zero; BigDecimal r76 = zero;
				BigDecimal r81 = zero; BigDecimal r82 = zero; BigDecimal r83 = zero;	BigDecimal r84 = zero;	BigDecimal r85 = zero; BigDecimal r86 = zero;
				BigDecimal r91 = zero; BigDecimal r92 = zero; BigDecimal r93 = zero;	BigDecimal r94 = zero;	BigDecimal r95 = zero; BigDecimal r96 = zero;
				BigDecimal r101 = zero; BigDecimal r102 = zero; BigDecimal r103 = zero;	BigDecimal r104 = zero;	BigDecimal r105 = zero; BigDecimal r106 = zero;
				BigDecimal r111 = zero; BigDecimal r112 = zero; BigDecimal r113 = zero;	BigDecimal r114 = zero;	BigDecimal r115 = zero; BigDecimal r116 = zero;
				BigDecimal r121 = zero; BigDecimal r122 = zero; BigDecimal r123 = zero;	BigDecimal r124 = zero;	BigDecimal r125 = zero; BigDecimal r126 = zero;
				BigDecimal r131 = zero; BigDecimal r132 = zero; BigDecimal r133 = zero;	BigDecimal r134 = zero;	BigDecimal r135 = zero; BigDecimal r136 = zero;


				// 1 ---------------------
				codes =	"1000000,1000001";	
				r11 = getData16(codes, d1, null, 1);
				r21 = getData16(codes, d1, null, 2);
				r31 = r11.subtract(r21);
				r41 = getData16(codes, d1, d2, 4);
				r51 = getData16(codes, d1, d2, 5);
				r61 = getData16(codes, d1, d2, 6);
				r71 = getData16(codes, d1, d2, 7);
				r81 = getData16(codes, d1, d2, 8);
				r91 = getData16(codes, d1, d2, 9);
				r101 = getData16(codes, d1, d2, 10);
				
				r121 = getData16(codes, null, d2, 12);
				r131 = getData16(codes, null, d2, 13);
				r111 = r121.subtract(r131);
				
				// 2 ---------------------
				codes =	"1000002";	
				r12 = getData16(codes, d1, null, 1);
				r22 = getData16(codes, d1, null, 2);
				r32 = r12.subtract(r22);
				r42 = getData16(codes, d1, d2, 4);
				r52 = getData16(codes, d1, d2, 5);
				r62 = getData16(codes, d1, d2, 6);
				r72 = getData16(codes, d1, d2, 7);
				r82 = getData16(codes, d1, d2, 8);
				r92 = getData16(codes, d1, d2, 9);
				r102 = getData16(codes, d1, d2, 10);
				
				r122 = getData16(codes, null, d2, 12);
				r132 = getData16(codes, null, d2, 13);
				r112 = r122.subtract(r132);
				
				// 3 ---------------------
				codes =	"1000003";	
				r13 = getData16(codes, d1, null, 1);
				r23 = getData16(codes, d1, null, 2);
				r33 = r13.subtract(r23);
				r43 = getData16(codes, d1, d2, 4);
				r53 = getData16(codes, d1, d2, 5);
				r63 = getData16(codes, d1, d2, 6);
				r73 = getData16(codes, d1, d2, 7);
				r83 = getData16(codes, d1, d2, 8);
				r93 = getData16(codes, d1, d2, 9);
				r103 = getData16(codes, d1, d2, 10);
				
				r123 = getData16(codes, null, d2, 12);
				r133 = getData16(codes, null, d2, 13);
				r113 = r123.subtract(r133);
				
				// 4 ---------------------
				codes =	"1000004,1000005,1000006,1000008,1000009";	
				r14 = getData16(codes, d1, null, 1);
				r24 = getData16(codes, d1, null, 2);
				r34 = r14.subtract(r24);
				r44 = getData16(codes, d1, d2, 4);
				r54 = getData16(codes, d1, d2, 5);
				r64 = getData16(codes, d1, d2, 6);
				r74 = getData16(codes, d1, d2, 7);
				r84 = getData16(codes, d1, d2, 8);
				r94 = getData16(codes, d1, d2, 9);
				r104 = getData16(codes, d1, d2, 10);
				
				r124 = getData16(codes, null, d2, 12);
				r134 = getData16(codes, null, d2, 13);
				r114 = r124.subtract(r134);
				
				// 5 ---------------------
				codes =	"1000013";	
				r15 = getData16(codes, d1, null, 1);
				r25 = getData16(codes, d1, null, 2);
				r35 = r15.subtract(r25);
				r45 = getData16(codes, d1, d2, 4);
				r55 = getData16(codes, d1, d2, 5);
				r65 = getData16(codes, d1, d2, 6);
				r75 = getData16(codes, d1, d2, 7);
				r85 = getData16(codes, d1, d2, 8);
				r95 = getData16(codes, d1, d2, 9);
				r105 = getData16(codes, d1, d2, 10);
				
				r125 = getData16(codes, null, d2, 12);
				r135 = getData16(codes, null, d2, 13);
				r115 = r125.subtract(r135);
				
				// 6 ---------------------
				r16 = r11.add(r12).add(r13).add(r14).add(r15); 
				r26 = r21.add(r22).add(r23).add(r24).add(r25);
				r36 = r31.add(r32).add(r33).add(r34).add(r35);
				r46 = r41.add(r42).add(r43).add(r44).add(r45);
				r56 = r51.add(r52).add(r53).add(r54).add(r55);
				r66 = r61.add(r62).add(r63).add(r64).add(r65);
				r76 = r71.add(r72).add(r73).add(r74).add(r75);
				r86 = r81.add(r82).add(r83).add(r84).add(r85);
				r96 = r91.add(r92).add(r93).add(r94).add(r95);
				r106 = r101.add(r102).add(r103).add(r104).add(r105);
				
				r126 = r121.add(r122).add(r123).add(r124).add(r125);
				r136 = r131.add(r132).add(r133).add(r134).add(r135);
				r116 = r126.subtract(r136);
				
				
				
				
				Line line;
				/*	1	*/	line = new Line();		
							line.setValue1(getDiv1000(r11).toString());	line.setValue2(getDiv1000(r12).toString());	line.setValue3(getDiv1000(r13).toString());
							line.setValue4(getDiv1000(r14).toString());	line.setValue5(getDiv1000(r15).toString());	line.setValue6(getDiv1000(r16).toString());
							list.add(line);
				/*	2	*/	line = new Line();		
							line.setValue1(getDiv1000(r21).toString());	line.setValue2(getDiv1000(r22).toString());	line.setValue3(getDiv1000(r23).toString());
							line.setValue4(getDiv1000(r24).toString());	line.setValue5(getDiv1000(r25).toString());	line.setValue6(getDiv1000(r26).toString());
							list.add(line);		
				/*	3	*/	line = new Line();		
							line.setValue1(getDiv1000(r31).toString());	line.setValue2(getDiv1000(r32).toString());	line.setValue3(getDiv1000(r33).toString());
							line.setValue4(getDiv1000(r34).toString());	line.setValue5(getDiv1000(r35).toString());	line.setValue6(getDiv1000(r36).toString());
							list.add(line);	
				/*	4	*/	line = new Line();		
							line.setValue1(getDiv1000(r41).toString());	line.setValue2(getDiv1000(r42).toString());	line.setValue3(getDiv1000(r43).toString());
							line.setValue4(getDiv1000(r44).toString());	line.setValue5(getDiv1000(r45).toString());	line.setValue6(getDiv1000(r46).toString());
							list.add(line);	
				/*	5	*/	line = new Line();		
							line.setValue1(getDiv1000(r51).toString());	line.setValue2(getDiv1000(r52).toString());	line.setValue3(getDiv1000(r53).toString());
							line.setValue4(getDiv1000(r54).toString());	line.setValue5(getDiv1000(r55).toString());	line.setValue6(getDiv1000(r56).toString());
							list.add(line);
				/*	6	*/	line = new Line();		
							line.setValue1(getDiv1000(r61).toString());	line.setValue2(getDiv1000(r62).toString());	line.setValue3(getDiv1000(r63).toString());
							line.setValue4(getDiv1000(r64).toString());	line.setValue5(getDiv1000(r65).toString());	line.setValue6(getDiv1000(r66).toString());
							list.add(line);							
							
				/*	7	*/	line = new Line();		
							line.setValue1(getDiv1000(r71).toString());	line.setValue2(getDiv1000(r72).toString());	line.setValue3(getDiv1000(r73).toString());
							line.setValue4(getDiv1000(r74).toString());	line.setValue5(getDiv1000(r75).toString());	line.setValue6(getDiv1000(r76).toString());
							list.add(line);							
				/*	8	*/	line = new Line();		
							line.setValue1(getDiv1000(r81).toString());	line.setValue2(getDiv1000(r82).toString());	line.setValue3(getDiv1000(r83).toString());
							line.setValue4(getDiv1000(r84).toString());	line.setValue5(getDiv1000(r85).toString());	line.setValue6(getDiv1000(r86).toString());
							list.add(line);							
				/*	9	*/	line = new Line();		
							line.setValue1(getDiv1000(r91).toString());	line.setValue2(getDiv1000(r92).toString());	line.setValue3(getDiv1000(r93).toString());
							line.setValue4(getDiv1000(r94).toString());	line.setValue5(getDiv1000(r95).toString());	line.setValue6(getDiv1000(r96).toString());
							list.add(line);							
				/*	10	*/	line = new Line();		
							line.setValue1(getDiv1000(r101).toString());	line.setValue2(getDiv1000(r102).toString());	line.setValue3(getDiv1000(r103).toString());
							line.setValue4(getDiv1000(r104).toString());	line.setValue5(getDiv1000(r105).toString());	line.setValue6(getDiv1000(r106).toString());
							list.add(line);							
				/*	11	*/	line = new Line();		
							line.setValue1(getDiv1000(r111).toString());	line.setValue2(getDiv1000(r112).toString());	line.setValue3(getDiv1000(r113).toString());
							line.setValue4(getDiv1000(r114).toString());	line.setValue5(getDiv1000(r115).toString());	line.setValue6(getDiv1000(r116).toString());
							list.add(line);							
				/*	12	*/	line = new Line();		
							line.setValue1(getDiv1000(r121).toString());	line.setValue2(getDiv1000(r122).toString());	line.setValue3(getDiv1000(r123).toString());
							line.setValue4(getDiv1000(r124).toString());	line.setValue5(getDiv1000(r125).toString());	line.setValue6(getDiv1000(r126).toString());
							list.add(line);							
				/*	13	*/	line = new Line();		
							line.setValue1(getDiv1000(r131).toString());	line.setValue2(getDiv1000(r132).toString());	line.setValue3(getDiv1000(r133).toString());
							line.setValue4(getDiv1000(r134).toString());	line.setValue5(getDiv1000(r135).toString());	line.setValue6(getDiv1000(r136).toString());
							list.add(line);							
							
				return list;
			}

			
			private BigDecimal getData16(String codes, Date dateFrom, Date dateTo, int type){
				BigDecimal result = zero;
				StringBuffer sql = new StringBuffer();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	a.a_asset_group_id  = "+words[i]+"	"; 
						else
							whereCodes += "	a.a_asset_group_id = "+words[i]+" or ";
							
					}
					whereCodes += " ) ";
						
				}

//				//prepare sql
				if(whereCodes.equals(""))
					return result;

				switch(type){
					case 1: //Первоначальная стоимость
						sql.append(
								"	select coalesce(sum(t.AssetAmt),0) a1	\n"	+
								"		from adempiere.a_asset a	\n"	+	
								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
								"		where  t.DateAcct <= '"+dateFrom+"' 	\n"	+ 
								"			and t.isActive = 'Y'	\n"	+
								"   		and a.isActive = 'Y'	\n"	+									
								"		" + whereCodes
								);
					break;	
					case 2://Накопленная амортизация
						sql.append(					
//								"	SELECT coalesce(sum(d.Expense),0) a1	\n"	+
//								"	from adempiere.A_Asset a	\n"	+
//								"	left join adempiere.A_Depreciation_Exp d on d.a_asset_id = a.a_asset_id	\n"	+
//								"	WHERE d.DateAcct <= '"+dateFrom+"'" + 
//								"	and d.isActive = 'Y'	\n"	+
//								"	and a.isActive = 'Y'	\n"	+
//								"		" + whereCodes
								"select sum(mm.amt) from (\n" +
								"	SELECT \n" +
								"		coalesce(sum(d.Expense),0) amt	\n" +
								"	from adempiere.A_Asset a	\n" +
								"	left join adempiere.A_Depreciation_Exp d on d.a_asset_id = a.a_asset_id	\n" +
								"	WHERE d.DateAcct <= '"+ dateFrom +"'\n" +
								"		and d.isActive = 'Y'\n" +
								"		and a.isActive = 'Y'\n" +
								"		" + whereCodes + 
								"		\n" +
								"	union all\n" +
								"	select \n" +
								"		sum(l.DeprAmt) amt\n" +
								"	from adempiere.A_Asset_Transaction h \n" +
								"	inner join adempiere.A_Asset_TransactionLine l on l.A_Asset_Transaction_id = h.A_Asset_Transaction_id\n" +
								"	left join adempiere.A_Asset a on a.a_asset_id = l.a_asset_id or l.a_asset2_id = a.a_asset_id\n" +
								"	left join adempiere.c_doctype d on d.c_doctype_id = h.c_doctype_id\n" +
								"	where   \n" +
								"			h.DateAcct < '"+dateFrom+"'\n" +
								"		and d.isActive = 'Y'\n" +
								"		and a.isActive = 'Y'\n" +
								"		and h.isActive = 'Y'\n" +
								"		and l.isActive = 'Y'	\n" +
								"		" + whereCodes + 
								"	\n" +
								")mm"								
								);
					break;	
					case 4: //Поступления
						sql.append(
								"	select coalesce(sum(t.AssetAmt),0) a1	\n"	+
								"		from adempiere.a_asset a	\n"	+
								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
								"		left join adempiere.A_Asset_Transaction	h on h.A_Asset_Transaction_ID = t.A_Asset_Transaction_ID \n"	+
								"		where  t.DateAcct between '"+dateFrom+"' and '"+dateTo+"'	\n"	+ 
								"			and t.isActive = 'Y'	\n"	+
								"   		and a.isActive = 'Y'	\n"	+
								"			and h.C_Doctype_ID = 1000005	\n"	+								
								"		" + whereCodes
								);
					break;	
//					case 5: //Выбытия
//						sql.append(
//								"	select coalesce(sum(t.AssetAmt),0) a1	\n"	+
//								"		from adempiere.a_asset a	\n"	+
//								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
//								"		left join adempiere.A_Asset_Transaction	h on h.A_Asset_Transaction_ID = t.A_Asset_Transaction_ID \n"	+
//								"		where  t.DateAcct between '"+dateFrom+"' and '"+dateTo+"'	\n"	+ 
//								"			and t.isActive = 'Y'	\n"	+
//								"   		and a.isActive = 'Y'	\n"	+
//								"			and h.C_Doctype_ID in (1000002,1000066)	\n"	+								
//								"		" + whereCodes
//								);
//					break;	
					case 6://Расходы по износу
						sql.append(					
								"	SELECT coalesce(sum(d.Expense),0) a1	\n"	+
								"	from adempiere.A_Asset a	\n"	+
								"	left join adempiere.A_Depreciation_Exp d on d.a_asset_id = a.a_asset_id	\n"	+
								"	WHERE d.DateAcct between '"+dateFrom+"' and '"+dateTo+"'	\n"	+
								"	and d.isActive = 'Y'	\n"	+
								"	and a.isActive = 'Y'	\n"	+
								"		" + whereCodes
								);
					break;	
					case 7: //Износ по выбытиям
						sql.append(
								"	select coalesce(sum(t.DeprAmt),0) a1	\n"	+
								"		from adempiere.a_asset a	\n"	+
								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
								"		left join adempiere.A_Asset_Transaction	h on h.A_Asset_Transaction_ID = t.A_Asset_Transaction_ID \n"	+
								"		where  t.DateAcct between '"+dateFrom+"' and '"+dateTo+"'	\n"	+ 
								"			and t.isActive = 'Y'	\n"	+
								"   		and a.isActive = 'Y'	\n"	+
								"			and h.C_Doctype_ID = 1000005	\n"	+								
								"		" + whereCodes
								);
					break;						
					case 8: //Износ по выбытиям
						sql.append(
								"	select coalesce(sum(t.AssetAmt),0) a1	\n"	+
								"		from adempiere.a_asset a	\n"	+
								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
								"		left join adempiere.A_Asset_Transaction	h on h.A_Asset_Transaction_ID = t.A_Asset_Transaction_ID \n"	+
								"		where  t.DateAcct between '"+dateFrom+"' and '"+dateTo+"'	\n"	+ 
								"			and t.isActive = 'Y'	\n"	+
								"   		and a.isActive = 'Y'	\n"	+
								"			and h.C_Doctype_ID in (1000002,1000066)	\n"	+								
								"		" + whereCodes
								);
					break;						
					//9	Обесценение
					//10Переводы и реклассификация
					//11Прочее
					
					case 12: //остаточная стоимость
						sql.append(
								"	select coalesce(sum(t.AssetAmt),0) a1	\n"	+
								"		from adempiere.a_asset a	\n"	+	
								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
								"		where  t.DateAcct < '"+dateTo+"' 	\n"	+ 
								"			and t.isActive = 'Y'	\n"	+
								"   		and a.isActive = 'Y'	\n"	+									
								"		" + whereCodes
								);
					break;	
					case 13: //Накопленная амортизация
						sql.append(					
								"select sum(mm.amt) from (\n" +
								"	SELECT \n" +
								"		coalesce(sum(d.Expense),0) amt	\n" +
								"	from adempiere.A_Asset a	\n" +
								"	left join adempiere.A_Depreciation_Exp d on d.a_asset_id = a.a_asset_id	\n" +
								"	WHERE d.DateAcct <= '"+ dateTo +"'\n" +
								"		and d.isActive = 'Y'\n" +
								"		and a.isActive = 'Y'\n" +
								"		" + whereCodes + 
								"		\n" +
								"	union all\n" +
								"	select \n" +
								"		sum(l.DeprAmt) amt\n" +
								"	from adempiere.A_Asset_Transaction h \n" +
								"	inner join adempiere.A_Asset_TransactionLine l on l.A_Asset_Transaction_id = h.A_Asset_Transaction_id\n" +
								"	left join adempiere.A_Asset a on a.a_asset_id = l.a_asset_id or l.a_asset2_id = a.a_asset_id\n" +
								"	left join adempiere.c_doctype d on d.c_doctype_id = h.c_doctype_id\n" +
								"	where   \n" +
								"			h.DateAcct <= '"+dateTo+"'\n" +
								"		and d.isActive = 'Y'\n" +
								"		and a.isActive = 'Y'\n" +
								"		and h.isActive = 'Y'\n" +
								"		and l.isActive = 'Y'	\n" +
								"		" + whereCodes + 
								"	\n" +
								")mm"	
								);
					break;						
					default :
						sql.append("select 0 as a1");
				}
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
//				if(result.compareTo(zero) != 0 )
//					result = result.divide(new BigDecimal(1000));
//				
//				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
				
				return result; 
			}	
			
			private List<Line> load17(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
				
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodFrom();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);

				int C_Period_ID2 = period.getPeriodTo();
				MPeriod p2 = new MPeriod(Env.getCtx(), C_Period_ID2, null);

				Date d1 = p.getStartDate();
				Date d2 = p2.getEndDate();

				
				BigDecimal r13 = zero; 	BigDecimal r23 = zero; BigDecimal r33 = zero;	BigDecimal r43 = zero;	BigDecimal r53 = zero;
				BigDecimal r63 = zero; 	BigDecimal r73 = zero; BigDecimal r83 = zero;	BigDecimal r93 = zero;	BigDecimal r103 = zero;
				BigDecimal r113 = zero;	BigDecimal r123 = zero;BigDecimal r133 = zero; 

				BigDecimal r14 = zero; 	BigDecimal r24 = zero; BigDecimal r34 = zero;	BigDecimal r44 = zero;	BigDecimal r54 = zero;
				BigDecimal r64 = zero; 	BigDecimal r74 = zero; BigDecimal r84 = zero;	BigDecimal r94 = zero;	BigDecimal r104 = zero;
				BigDecimal r114 = zero;	BigDecimal r124 = zero;BigDecimal r134 = zero;
				
				// 1 ---------------------
				r13 = getData17("1000007", d1, null, 1);
				r23 = getData17("1000007", d1, null, 2);
				r33 = r13.subtract(r23);

				r43 = getData17("1000007", d1, d2, 4);
				r53 = getData17("1000007", d1, d2, 5);
				r63 = getData17("1000007", d1, d2, 6);
				r73 = getData17("1000007", d1, d2, 7);
				r83 = getData17("1000007", d1, d2, 8);
				r93 = getData17("1000007", d1, d2, 9);
				r103 = getData17("1000007", d1, d2, 10);
				
				r123 = getData17("1000007", null, d2, 12);
				r133 = getData17("1000007", null, d2, 13);
				r113 = r123.subtract(r133);

				//4 - dvelopming
				r14 = getData17("1000010", d1, null, 1);
				r24 = getData17("1000010", d1, null, 2);
				r34 = r14.subtract(r24);

				r44 = getData17("1000010", d1, d2, 4);
				r54 = getData17("1000010", d1, d2, 5);
				r64 = getData17("1000010", d1, d2, 6);
				r74 = getData17("1000010", d1, d2, 7);
				r84 = getData17("1000010", d1, d2, 8);
				r94 = getData17("1000010", d1, d2, 9);
				r104 = getData17("1000010", d1, d2, 10);
				
				r124 = getData17("1000010", null, d2, 12);
				r134 = getData17("1000010", null, d2, 13);
				r114 = r124.subtract(r134);
				
				
				// 3 ---------------------
//				r13 = getData17("273000", d1, null, "dr");
//				r23 = getData17("274000", d1, d2, "cr");
//				r33 = r13.subtract(r23);
//				r43 = getData17("273000", d1, d2, "dr");
//				r53 = getData17("273000", d1, d2, "cr");
//				r63 = getData17("274000", d1, d2, "cr");
//				r73 = getData17("741000", d1, d2, "dr");	r73 = r73.add(getData17("273000", d1, d2, "cr"));
//				r83 = getData17("742000", d1, d2, "dr");	r83 = r83.add(getData17("273000", d1, d2, "dr"));
//				r93 = zero;
//				r103 = getData17("747000", d1, d2, "dr");	r103 = r103.add(getData17("273000", d1, d2, "cr"));
//				r113 = r33.add(r43).add(r53).add(r63).add(r73).add(r83).add(r93).add(r103);
//				r123 = getData17("273000", null, d2, "dr");
//				r133 = getData17("274000", null, d2, "cr");
				
				
				Line line;
				/*	1	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r13).toString());
							line.setValue4(getDiv1000(r14).toString());	line.setValue5(getDiv1000(r13.add(r14)).toString());	
							list.add(line);
				/*	2	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r23).toString());
							line.setValue4(getDiv1000(r24).toString());	line.setValue5(getDiv1000(r23.add(r24)).toString());	
							list.add(line);		
				/*	3	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r33).toString());
							line.setValue4(getDiv1000(r34).toString());	line.setValue5(getDiv1000(r33.add(r34)).toString());	
							list.add(line);	
				/*	4	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r43).toString());
							line.setValue4(getDiv1000(r44).toString());	line.setValue5(getDiv1000(r43.add(r44)).toString());	
							list.add(line);	
				/*	5	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r53).toString());
							line.setValue4(getDiv1000(r54).toString());	line.setValue5(getDiv1000(r53.add(r54)).toString());	
							list.add(line);
				/*	6	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r63).toString());
							line.setValue4(getDiv1000(r64).toString());	line.setValue5(getDiv1000(r63.add(r64)).toString());	
							list.add(line);							
							
				/*	7	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r73).toString());
							line.setValue4(getDiv1000(r74).toString());	line.setValue5(getDiv1000(r73.add(r74)).toString());	
							list.add(line);							
				/*	8	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r83).toString());
							line.setValue4(getDiv1000(r84).toString());	line.setValue5(getDiv1000(r83.add(r84)).toString());	
							list.add(line);							
				/*	9	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r93).toString());
							line.setValue4(getDiv1000(r94).toString());	line.setValue5(getDiv1000(r93.add(r94)).toString());	
							list.add(line);							
				/*	10	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r103).toString());
							line.setValue4(getDiv1000(r104).toString());	line.setValue5(getDiv1000(r103.add(r104)).toString());	
							list.add(line);							
				/*	11	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r113).toString());
							line.setValue4(getDiv1000(r114).toString());	line.setValue5(getDiv1000(r113.add(r114)).toString());	
							list.add(line);							
				/*	12	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r123).toString());
							line.setValue4(getDiv1000(r124).toString());	line.setValue5(getDiv1000(r123.add(r124)).toString());	
							list.add(line);							
				/*	13	*/	line = new Line();		
							line.setValue1(zero.toString());	line.setValue2(zero.toString());	line.setValue3(getDiv1000(r133).toString());
							line.setValue4(getDiv1000(r134).toString());	line.setValue5(getDiv1000(r133.add(r134)).toString());	
							list.add(line);							
							
				return list;
			}

			
			private BigDecimal getData17(String codes, Date dateFrom, Date dateTo, int type){
				BigDecimal result = zero;
				StringBuffer sql = new StringBuffer();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	a.a_asset_group_id  = "+words[i]+"	"; 
						else
							whereCodes += "	a.a_asset_group_id = "+words[i]+" or ";
							
					}
					whereCodes += " ) ";
						
				}

//				//prepare sql
				if(whereCodes.equals(""))
					return result;

				switch(type){
					case 1: //Первоначальная стоимость
						sql.append(
								"	select coalesce(sum(t.AssetAmt),0) a1	\n"	+
								"		from adempiere.a_asset a	\n"	+
								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
								"		left join adempiere.A_Asset_Transaction	h on h.A_Asset_Transaction_ID = t.A_Asset_Transaction_ID \n"	+
								"		where  t.DateAcct <= '"+dateFrom+"'	\n"	+ 
								"			and t.isActive = 'Y'	\n"	+
								"   		and a.isActive = 'Y'	\n"	+
								"			and h.C_Doctype_ID in (1000065, 1000005)	\n"	+								
								"		" + whereCodes
								);
					break;	
					case 2://Накопленная амортизация
						sql.append(					
//								"	SELECT coalesce(sum(d.Expense),0) a1	\n"	+
//								"	from adempiere.A_Asset a	\n"	+
//								"	left join adempiere.A_Depreciation_Exp d on d.a_asset_id = a.a_asset_id	\n"	+
//								"	WHERE d.DateAcct <= '"+dateFrom+"'" + 
//								"	and d.isActive = 'Y'	\n"	+
//								"	and a.isActive = 'Y'	\n"	+
//								"		" + whereCodes
								"select sum(mm.amt) from (\n" +
								"	SELECT \n" +
								"		coalesce(sum(d.Expense),0) amt	\n" +
								"	from adempiere.A_Asset a	\n" +
								"	left join adempiere.A_Depreciation_Exp d on d.a_asset_id = a.a_asset_id	\n" +
								"	WHERE d.DateAcct <= '"+ dateFrom +"'\n" +
								"		and d.isActive = 'Y'\n" +
								"		and a.isActive = 'Y'\n" +
								"		" + whereCodes + 
								"		\n" +
								"	union all\n" +
								"	select \n" +
								"		sum(l.DeprAmt) amt\n" +
								"	from adempiere.A_Asset_Transaction h \n" +
								"	inner join adempiere.A_Asset_TransactionLine l on l.A_Asset_Transaction_id = h.A_Asset_Transaction_id\n" +
								"	left join adempiere.A_Asset a on a.a_asset_id = l.a_asset_id or l.a_asset2_id = a.a_asset_id\n" +
								"	left join adempiere.c_doctype d on d.c_doctype_id = h.c_doctype_id\n" +
								"	where   \n" +
								"			h.DateAcct < '"+dateFrom+"'\n" +
								"		and d.isActive = 'Y'\n" +
								"		and a.isActive = 'Y'\n" +
								"		and h.isActive = 'Y'\n" +
								"		and l.isActive = 'Y'	\n" +
								"		" + whereCodes + 
								"	\n" +
								")mm"								
								);
					break;	
					case 4: //Поступления
						sql.append(
								"	select coalesce(sum(t.AssetAmt),0) a1	\n"	+
								"		from adempiere.a_asset a	\n"	+
								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
								"		left join adempiere.A_Asset_Transaction	h on h.A_Asset_Transaction_ID = t.A_Asset_Transaction_ID \n"	+
								"		where  t.DateAcct between '"+dateFrom+"' and '"+dateTo+"'	\n"	+ 
								"			and t.isActive = 'Y'	\n"	+
								"   		and a.isActive = 'Y'	\n"	+
								"			and h.C_Doctype_ID = 1000005	\n"	+								
								"		" + whereCodes
								);
					break;	
//					case 5: //Выбытия
//						sql.append(
//								"	select coalesce(sum(t.AssetAmt),0) a1	\n"	+
//								"		from adempiere.a_asset a	\n"	+
//								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
//								"		left join adempiere.A_Asset_Transaction	h on h.A_Asset_Transaction_ID = t.A_Asset_Transaction_ID \n"	+
//								"		where  t.DateAcct between '"+dateFrom+"' and '"+dateTo+"'	\n"	+ 
//								"			and t.isActive = 'Y'	\n"	+
//								"   		and a.isActive = 'Y'	\n"	+
//								"			and h.C_Doctype_ID in (1000002,1000066)	\n"	+								
//								"		" + whereCodes
//								);
//					break;	
					case 6://Расходы по износу
						sql.append(					
								"	SELECT coalesce(sum(d.Expense),0) a1	\n"	+
								"	from adempiere.A_Asset a	\n"	+
								"	left join adempiere.A_Depreciation_Exp d on d.a_asset_id = a.a_asset_id	\n"	+
								"	WHERE d.DateAcct between '"+dateFrom+"' and '"+dateTo+"'	\n"	+
								"	and d.isActive = 'Y'	\n"	+
								"	and a.isActive = 'Y'	\n"	+
								"		" + whereCodes
								);
					break;	
					case 7: //Износ по выбытиям
						sql.append(
								"	select coalesce(sum(t.DeprAmt),0) a1	\n"	+
								"		from adempiere.a_asset a	\n"	+
								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
								"		left join adempiere.A_Asset_Transaction	h on h.A_Asset_Transaction_ID = t.A_Asset_Transaction_ID \n"	+
								"		where  t.DateAcct between '"+dateFrom+"' and '"+dateTo+"'	\n"	+ 
								"			and t.isActive = 'Y'	\n"	+
								"   		and a.isActive = 'Y'	\n"	+
								"			and h.C_Doctype_ID = 1000005	\n"	+								
								"		" + whereCodes
								);
					break;						
					case 8: //Износ по выбытиям
						sql.append(
								"	select coalesce(sum(t.AssetAmt),0) a1	\n"	+
								"		from adempiere.a_asset a	\n"	+
								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
								"		left join adempiere.A_Asset_Transaction	h on h.A_Asset_Transaction_ID = t.A_Asset_Transaction_ID \n"	+
								"		where  t.DateAcct between '"+dateFrom+"' and '"+dateTo+"'	\n"	+ 
								"			and t.isActive = 'Y'	\n"	+
								"   		and a.isActive = 'Y'	\n"	+
								"			and h.C_Doctype_ID in (1000002,1000066)	\n"	+								
								"		" + whereCodes
								);
					break;						
					//9	Обесценение
					//10Переводы и реклассификация
					//11Прочее
					
					case 12: //остаточная стоимость
						sql.append(
								"	select coalesce(sum(t.AssetAmt),0) a1	\n"	+
								"		from adempiere.a_asset a	\n"	+
								"		left join adempiere.A_Asset_TransactionLine t on t.a_asset_id = a.a_asset_id or t.a_asset2_id = a.a_asset_id	\n"	+
								"		left join adempiere.A_Asset_Transaction	h on h.A_Asset_Transaction_ID = t.A_Asset_Transaction_ID \n"	+
								"		where  t.DateAcct <= '"+dateTo+"'	\n"	+ 
								"			and t.isActive = 'Y'	\n"	+
								"   		and a.isActive = 'Y'	\n"	+
								"			and h.C_Doctype_ID in (1000065, 1000005)	\n"	+								
								"		" + whereCodes
								);
					break;	
					case 13: //Накопленная амортизация
						sql.append(					
								"select sum(mm.amt) from (\n" +
								"	SELECT \n" +
								"		coalesce(sum(d.Expense),0) amt	\n" +
								"	from adempiere.A_Asset a	\n" +
								"	left join adempiere.A_Depreciation_Exp d on d.a_asset_id = a.a_asset_id	\n" +
								"	WHERE d.DateAcct <= '"+ dateTo +"'\n" +
								"		and d.isActive = 'Y'\n" +
								"		and a.isActive = 'Y'\n" +
								"		" + whereCodes + 
								"		\n" +
								"	union all\n" +
								"	select \n" +
								"		sum(l.DeprAmt) amt\n" +
								"	from adempiere.A_Asset_Transaction h \n" +
								"	inner join adempiere.A_Asset_TransactionLine l on l.A_Asset_Transaction_id = h.A_Asset_Transaction_id\n" +
								"	left join adempiere.A_Asset a on a.a_asset_id = l.a_asset_id or l.a_asset2_id = a.a_asset_id\n" +
								"	left join adempiere.c_doctype d on d.c_doctype_id = h.c_doctype_id\n" +
								"	where   \n" +
								"			h.DateAcct <= '"+dateTo+"'\n" +
								"		and d.isActive = 'Y'\n" +
								"		and a.isActive = 'Y'\n" +
								"		and h.isActive = 'Y'\n" +
								"		and l.isActive = 'Y'	\n" +
								"		" + whereCodes + 
								"	\n" +
								")mm"	
								);
					break;						
					default :
						sql.append("select 0 as a1");
				}
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1).abs();
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}				
//				BigDecimal result = zero;
//				StringBuffer sql = new StringBuffer();
//				
//				String[] words = codes.split(",");  
//				
//				String whereCodes = "";
//				if(words.length > 0){
//					whereCodes = " and ( ";
//					for(int i=0; i < words.length; i++){
//						if(i == (words.length-1))
//							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
//						else
//							whereCodes += "	ce.value like '"+words[i]+"%' or ";
//							
//					}
//					whereCodes += " ) ";
//						
//				}
//
////				//prepare sql
//				if(whereCodes.equals(""))
//					return result;
//
//				if(type.equals("saldo"))
//					sql.append("	select coalesce(sum(fa.amtacctcr + fa.amtacctdr),0)	\n");
//				else if(type.equals("cr"))
//					sql.append("	select coalesce(sum(fa.amtacctcr),0)	\n");
//				else
//					sql.append("	select coalesce(sum(fa.amtacctdr),0)	\n");
//				
//				sql.append(
//						"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n"	 
//						);
//
//				if(dateFrom != null && dateTo != null){	
//					sql.append(	"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	);
//				}else if(dateTo == null){
//					sql.append(	"		and fa.dateacct <= '"+dateFrom+"'::date 	\n"	);
//				}if(dateFrom == null){
//					sql.append(	"		and fa.dateacct <= '"+dateTo+"'::date	\n"	);
//				}
//				
//					sql.append(whereCodes);				
//				
//				
//				
//				PreparedStatement pstmt = null;
//				ResultSet rs = null;
//				
//				try {
//					pstmt = DB.prepareStatement(sql.toString(), null);
//					rs = pstmt.executeQuery();
//					while (rs.next()) {
//							result = rs.getBigDecimal(1);
//					}
//				} catch (SQLException e) {
//					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
//				} finally {
//					DB.close(rs, pstmt);
//					rs = null;
//					pstmt = null;
//				}
//				
//				if(result.compareTo(zero) != 0 )
//					result = result.divide(new BigDecimal(1000));
//				
//				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
//				
				return result; 
			}				
			
			private List<Line> load18(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
				
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodTo();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
				Long time;

				Timestamp ts1 = p.getEndDate();//begin of choosen period 
				//ts2
				Timestamp ts2 = p.getEndDate();//choosen period 
				
				//ts1
				Date d2 = new Date(ts2.getTime());
				Calendar c = Calendar.getInstance();
				c.setTime(new Date(ts2.getTime()));
				c.set(Calendar.DAY_OF_MONTH, 1);
				c.set(Calendar.MONTH, 0);
				Date d1 = new Date(c.getTimeInMillis());

				String codes = "";
				
				BigDecimal r1 = zero; 
				BigDecimal r2 = zero;
				BigDecimal r3 = zero;
				BigDecimal r4 = zero;
				BigDecimal rTotal = zero;
			
				codes =	"291000";	
				r1 = getData18(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
				
				codes = "292000";	
				r2 = getData18(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));

//				codes = "293001,293002,293003";	
//				r3 = getData18(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
				
				codes = "294000";	
				r4 = getData18(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
				
				rTotal = rTotal.add(r1).add(r2).add(r3).add(r4);
				
				Line line;
				/*	1	*/	line = new Line();		line.setValue1(r1.toString()); list.add(line);
				/*	2	*/	line = new Line();		line.setValue1(r2.toString()); list.add(line);		
				/*	3	*/	line = new Line();		line.setValue1(r3.toString()); list.add(line);
//				/*	4	*/	line = new Line();		line.setValue1(r4.toString()); list.add(line);
				/*	5	*/	line = new Line();		line.setValue1(rTotal.toString()); list.add(line);
				
				return list;
			}

			
			private BigDecimal getData18(String codes, Date dateFrom, Date dateTo){
				BigDecimal result = zero;
				StringBuffer sql = new StringBuffer();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
						else
							whereCodes += "	ce.value like '"+words[i]+"%' or ";
							
					}
					whereCodes += " ) ";
						
				}

//				//prepare sql
				if(whereCodes.equals(""))
					return result;

				sql.append(
						"	select coalesce(sum(fa.amtacctdr - fa.amtacctcr),0)	\n" +
						"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n"	+ 
						"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	+	
						"		" + whereCodes
						);
				
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
				if(result.compareTo(zero) != 0 )
					result = result.divide(new BigDecimal(1000));
				
				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
				
				return result; 
			}	

			private List<Line> load19(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
		 		List<Line> list2 = new ArrayList<Line>();
		 		
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodTo();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
				Long time;

				Timestamp ts1 = p.getEndDate();//begin of choosen period 
				//ts2
				Timestamp ts2 = p.getEndDate();//choosen period 
				
				//ts1
				Date d2 = new Date(ts2.getTime());
				Calendar c = Calendar.getInstance();
				c.setTime(new Date(ts2.getTime()));
				c.set(Calendar.DAY_OF_MONTH, 1);
				c.set(Calendar.MONTH, 0);
				Date d1 = new Date(c.getTimeInMillis());

				
				String codes = "331000,332000,333000,334000,336000,337000,338000,3390";
				list = getData19(new Timestamp(d1.getTime()), new Timestamp(d2.getTime()), codes);

				BigDecimal summ = Env.ZERO;
				
				// get total summ
				for(Line l: list){
					String sum  = l.getValue2();
					try{
						
						BigDecimal b = new BigDecimal(sum);
						
						if(b == null || b.compareTo(zero) == 0)
							continue;

						summ = summ.add(b);
					}catch(Exception e){}
				}
				
				Line line = new Line(); line.setValue4(getDiv1000(summ).toString()); list2.add(line);
				
				int rows = list.size();
				if(rows > 0){
					for(int i = 0; i < rows; i++){
						
						BigDecimal b = new BigDecimal(list.get(i).getValue2());
						
						if(b == null || b.compareTo(zero) == 0)
							continue;
						
						line  = new Line();
						line = new Line(); 
							line.setValue1(list.get(i).getValue1());
//							line.setValue2(list.get(i).getValue2());
//							line.setValue3(list.get(i).getValue3());
							line.setValue4(getDiv1000(new BigDecimal(list.get(i).getValue2())).toString());
						list2.add(line);
					}
				}
				
				
				
				return list2;
			}

			
			private List<Line> getData19(Date dateFrom, Date dateTo, String codes){
				StringBuffer sql = new StringBuffer();
				List<Line> result = new ArrayList<Line>();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
						else
							whereCodes += "	ce.value like '"+words[i]+"%' or ";
							
					}
					whereCodes += " ) ";
				}
				
				sql.append(
						"	select	\n"	+ 
//						"		b.name, fa.dateacct, fa.description, coalesce(fa.amtacctcr - fa.amtacctdr) summ	\n"	+
						"		b.name, sum(coalesce(fa.amtacctcr - fa.amtacctdr)) summ	\n"	+								
//						"		,(case when ce.value like '1290%' then coalesce(fa.amtacctcr - fa.amtacctdr) else 0 end) a129	\n"	+
						"	from 	adempiere.fact_acct fa	\n"	+
						"	inner join adempiere.c_elementvalue ce 	on ce.c_elementvalue_id  = fa.account_id	\n"	+
						"	left join adempiere.c_bpartner b on b.c_bpartner_id = fa.c_bpartner_id	\n"	+

						"	where fa.dateacct <= '"+	dateTo	+"'	\n"	
						+ whereCodes +
						"	group by b.name	\n" +  
						"	order by b.name	\n"	
						);
				
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				int i = 1;
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
						Line line = new Line();
						line.setValue1(rs.getString(1));
//						line.setValue2(rs.getDate(2).toString());
//						line.setValue3(rs.getString(2));
						
						BigDecimal t = Env.ZERO;
						t = rs.getBigDecimal(2);
//						if(t.compareTo(zero) < 0 )
//							t = t.abs();//divide(new BigDecimal(1000));
//						
//						t = t.setScale(0, BigDecimal.ROUND_HALF_UP);
						
						
						line.setValue2(t.toString());
//						line.setValue3(rs.getBigDecimal(3).toString());
						result.add(line);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}				
//				StringBuffer sql = new StringBuffer();
//				List<Line> result = new ArrayList<Line>();
//				
//				String[] words = codes.split(",");  
//				
//				String whereCodes = "";
//				if(words.length > 0){
//					whereCodes = " and ( ";
//					for(int i=0; i < words.length; i++){
//						if(i == (words.length-1))
//							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
//						else
//							whereCodes += "	ce.value like '"+words[i]+"%' or ";
//							
//					}
//					whereCodes += " ) ";
//				}
//				
//				sql.append(
//						"	select	\n"	+ 
//						"		b.name, fa.dateacct, fa.description, (coalesce(fa.amtacctcr,0) + coalesce(fa.amtacctdr)) summ	\n"	+
//
//						"	from 	adempiere.fact_acct fa	\n"	+
//						"	inner join adempiere.c_elementvalue ce 	on ce.c_elementvalue_id  = fa.account_id	\n"	+
//						"	left join adempiere.c_bpartner b on b.c_bpartner_id = fa.c_bpartner_id	\n"	+
//
//						"	where fa.dateacct <= '"+	dateTo	+"'	\n"	
//						+ whereCodes +
//						"	order by b.name	\n"	
//						);
//				
//				
//				PreparedStatement pstmt = null;
//				ResultSet rs = null;
//				int i = 1;
//				try {
//					pstmt = DB.prepareStatement(sql.toString(), null);
//					rs = pstmt.executeQuery();
//					while (rs.next()) {
//						Line line = new Line();
//						line.setValue1(rs.getString(1));
//						line.setValue2(rs.getDate(2).toString());
//						line.setValue3(rs.getString(3));
//						
//						BigDecimal t = Env.ZERO;
//						t = rs.getBigDecimal(4);
//						if(t.compareTo(zero) != 0 )
//							t = t.divide(new BigDecimal(1000));
//						
//						t = t.setScale(0, BigDecimal.ROUND_HALF_UP);
//						
//						
//						line.setValue4(t.toString());
//						result.add(line);
//					}
//				} catch (SQLException e) {
//					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
//				} finally {
//					DB.close(rs, pstmt);
//					rs = null;
//					pstmt = null;
//				}
				
				
				return result; 
			}				

			
			private List<Line> load20(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
				
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodTo();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
				Long time;

				Timestamp ts1 = p.getEndDate();//begin of choosen period 
				//ts2
				Timestamp ts2 = p.getEndDate();//choosen period 
				
				//ts1
				Date d2 = new Date(ts2.getTime());
				Calendar c = Calendar.getInstance();
				c.setTime(new Date(ts2.getTime()));
				c.set(Calendar.DAY_OF_MONTH, 1);
				c.set(Calendar.MONTH, 0);
				Date d1 = new Date(c.getTimeInMillis());

				String codes = " ";
				
				BigDecimal r1 = zero; BigDecimal r2 = zero; BigDecimal r3 = zero;	BigDecimal r4 = zero;
				BigDecimal rtotal = zero;
			
				codes =	"342000";	
				r1 = getData20(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),1,0);
//				codes =	"343000";	
				r2 = getData20(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),2,120);
//				codes =	"344000,341000";	
				r3 = getData20(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),2,110);

				codes =	"344000";	
				r4 = getData20(codes, null, new Timestamp(d2.getTime()),1,0);
				
				rtotal = rtotal.add(r1).add(r2).add(r3).add(r4);
				
				Line line;
				/*	1	*/	line = new Line();		line.setValue1(zero.toString()); list.add(line);
				/*	2	*/	line = new Line();		line.setValue1(zero.toString()); list.add(line);
				/*	3	*/	line = new Line();		line.setValue1(r1.toString()); list.add(line);
				/*	4	*/	line = new Line();		line.setValue1(r2.toString()); list.add(line);
				/*	5	*/	line = new Line();		line.setValue1(r3.toString()); list.add(line);
				/*	6	*/	line = new Line();		line.setValue1(r4.toString()); list.add(line);				
				/*	7	*/	line = new Line();		line.setValue1(rtotal.toString()); list.add(line);
				
				return list;
			}

			
			private BigDecimal getData20(String codes, Date dateFrom, Date dateTo, int type, int hrm_reserve_id){
				BigDecimal result = zero;
				StringBuffer sql = new StringBuffer();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
						else
							whereCodes += "	ce.value like '"+words[i]+"%' or ";
							
					}
					whereCodes += " ) ";
						
				}

//				//prepare sql
				if(whereCodes.equals(""))
					return result;
				switch(type){
				case 1:
					sql.append(
							"	select coalesce(sum(fa.amtacctdr - fa.amtacctcr),0)	\n" +
							"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n"	 
//							"		and fa.dateacct <= '"+dateTo+"'::date " +
//	//								"and '"+dateTo+"'::date	\n"	+	
//							"		" + whereCodes
							);
					if(dateFrom != null && dateTo != null){	
						sql.append(	"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	);
					}else if(dateTo == null){
						sql.append(	"		and fa.dateacct <= '"+dateFrom+"'::date 	\n"	);
					}if(dateFrom == null){
						sql.append(	"		and fa.dateacct <= '"+dateTo+"'::date	\n"	);
					}
					
						sql.append(whereCodes);				

//							);
					break;
				case 2:
					sql.append(
							" WITH pMain AS (WITH main AS (SELECT ad_client_id,ad_org_id,account_id,hrm_reserve_id as subkonto1_id, \n" +
									" SUM(amtacctdr) as dr1, SUM(amtacctcr) as cr1,0::numeric as dr2, 0::numeric as cr2 \n" +
									" FROM adempiere.v_fi_fact_acct_with_subconto \n" +
									" WHERE dateacct<'"+dateFrom+"' AND account_id= 1005507\n" +
									" GROUP BY ad_client_id,ad_org_id,account_id,hrm_reserve_id UNION ALL \n" +
									" SELECT ad_client_id,ad_org_id,account_id,hrm_reserve_id as subkonto1_id, \n" +
									" 0::numeric as dr1,0::numeric as cr1,SUM(amtacctdr) as dr2, SUM(amtacctcr) as cr2 \n" +
									" FROM adempiere.v_fi_fact_acct_with_subconto \n" +
									" WHERE dateacct BETWEEN '"+dateFrom+"' AND '"+dateTo+"' AND account_id=1005507 \n" +
									" GROUP BY ad_client_id,ad_org_id,account_id,hrm_reserve_id) \n" +
									" SELECT m.ad_client_id,m.ad_org_id,m.account_id,m.subkonto1_id,c.value as accvalue,reserve.name as subvalue,COALESCE(reserve.name,'<...>') as subname, \n" +
									" adempiere.func1(SUM(dr1),SUM(cr1),'dr') as dr1,adempiere.func1(SUM(dr1),SUM(cr1),'cr') as cr1, SUM(dr2) as dr2, SUM(cr2) as cr2, \n" +
									" adempiere.func1((SUM(dr1)+SUM(dr2)),(SUM(cr1)+SUM(cr2)),'dr') as dr3,adempiere.func1((SUM(dr1)+SUM(dr2)),(SUM(cr1)+SUM(cr2)),'cr') as cr3 \n" +
									" FROM main m \n" +
									" LEFT JOIN adempiere.hrm_reserve reserve ON(m.subkonto1_id = reserve.hrm_reserve_id) \n" +
									" LEFT JOIN adempiere.c_elementvalue c ON(m.account_id = c.c_elementvalue_id) \n" +
									" WHERE 1=1  GROUP BY m.ad_client_id,m.ad_org_id,m.account_id,m.subkonto1_id,c.value,reserve.name,reserve.name ORDER BY m.subkonto1_id)  \n" +
									" SELECT cr3 FROM pMain WHERE (dr1 > 0 OR cr1 > 0 OR dr2 > 0 OR cr2 > 0 OR dr3 > 0 OR cr3 > 0)\n" +
									" and subkonto1_id = "+hrm_reserve_id
								);
					break;
				}
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
				if(result.compareTo(zero) != 0 ){
					result = result.abs();
					result = result.divide(new BigDecimal(1000));
				}
				
				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
				
				return result; 
			}				
			
			private List<Line> load21(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
				
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodTo();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
				Long time;

				Timestamp ts1 = p.getEndDate();//begin of choosen period 
				//ts2
				Timestamp ts2 = p.getEndDate();//choosen period 
				
				//ts1
				Date d2 = new Date(ts2.getTime());
				Calendar c = Calendar.getInstance();
				c.setTime(new Date(ts2.getTime()));
				c.set(Calendar.DAY_OF_MONTH, 1);
				c.set(Calendar.MONTH, 0);
				Date d1 = new Date(c.getTimeInMillis());

				String codes = "";
				
				BigDecimal r1 = zero; BigDecimal r2 = zero; BigDecimal r3 = zero;
				BigDecimal rtotal = zero;
			
				codes =	"335000";	
				r1 = getData21(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//				codes =	"343000";	
//				r1 = getData20(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//				codes =	"3440*";	
//				r1 = getData20(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//				
//				rtotal = rtotal.add(r1).add(r2).add(r3);
				
				Line line;
				/*	1	*/	line = new Line();		line.setValue1(r1.abs().toString()); list.add(line);
				/*	2	*/	line = new Line();		line.setValue1(r1.abs().toString()); list.add(line);
				/*	3	*/	line = new Line();		line.setValue1(zero.toString()); list.add(line);
				
				return list;
			}

			
			private BigDecimal getData21(String codes, Date dateFrom, Date dateTo){
				BigDecimal result = zero;
				StringBuffer sql = new StringBuffer();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
						else
							whereCodes += "	ce.value like '"+words[i]+"%' or ";
							
					}
					whereCodes += " ) ";
						
				}

//				//prepare sql
				if(whereCodes.equals(""))
					return result;

				sql.append(
						"	select coalesce(sum(fa.amtacctdr - fa.amtacctcr),0)	\n" +
						"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n"	+ 
						"		and fa.dateacct <= '"+dateTo+"'::date	\n"	+	
						"		" + whereCodes
						);
				
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
				if(result.compareTo(zero) != 0 )
					result = result.divide(new BigDecimal(1000));
				
				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
				
				return result; 
			}				
			
			
			
			private List<Line> load22(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
				
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodTo();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
				Long time;

				Timestamp ts1 = p.getEndDate();//begin of choosen period 
				//ts2
				Timestamp ts2 = p.getEndDate();//choosen period 
				
				//ts1
				Date d2 = new Date(ts2.getTime());
				Calendar c = Calendar.getInstance();
				c.setTime(new Date(ts2.getTime()));
				c.set(Calendar.DAY_OF_MONTH, 1);
				c.set(Calendar.MONTH, 0);
				Date d1 = new Date(c.getTimeInMillis());

				String codes = "";
				
				BigDecimal r1 = zero; 
				BigDecimal r2 = zero;
				BigDecimal r3 = zero;
				BigDecimal r4 = zero;
				BigDecimal r5 = zero;
				BigDecimal rTotal = zero;
			
//				codes =	"351000";	
//				r1 = getData22(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//				
//				codes = "352000";	
//				r2 = getData22(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//
//				codes = "353000";	
//				r3 = getData22(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//				
//				codes = "354000";	
//				r4 = getData22(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));
//
//				codes = "312000,313000,314000,315000,316000,317000,318000,319000, 321000,322000,323000,324000";	
//				r5 = getData22(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()));

				
//				3500,312000,313000,314000,315000,316000,317000,318000,319000,3200
				
				codes =	"351000";	
				r1 = getDiv1000(getData(codes, new Timestamp(d2.getTime()), "cr", "")).abs();
				
				codes = "352000";	
				r2 = getDiv1000(getData(codes, new Timestamp(d2.getTime()), "cr", "")).abs();

				codes = "353000";	
				r3 = getDiv1000(getData(codes, new Timestamp(d2.getTime()), "cr", "")).abs();
				
				codes = "354000,341000";	
				r4 = getDiv1000(getData(codes, new Timestamp(d2.getTime()), "cr", "")).abs();

				codes = "312000,313000,314000,315000,316000,317000,318000,319000,323000,324000,321000,322000";	
				r5 = getDiv1000(getData(codes, new Timestamp(d2.getTime()), "cr", "")).abs();
				
				
				rTotal = rTotal.add(r1).add(r2).add(r3).add(r4).add(r5);
				
				Line line;
				/*	1	*/	line = new Line();		line.setValue1(r1.toString()); list.add(line);
				/*	2	*/	line = new Line();		line.setValue1(r2.toString()); list.add(line);		
				/*	3	*/	line = new Line();		line.setValue1(r3.toString()); list.add(line);
				/*	4	*/	line = new Line();		line.setValue1(r4.toString()); list.add(line);
				/*	5	*/	line = new Line();		line.setValue1(r5.toString()); list.add(line);
				/*	6	*/	line = new Line();		line.setValue1(rTotal.abs().toString()); list.add(line);
				
				return list;
			}

			
			private BigDecimal getData22(String codes, Date dateFrom, Date dateTo){
				BigDecimal result = zero;
				StringBuffer sql = new StringBuffer();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
						else
							whereCodes += "	ce.value like '"+words[i]+"%' or ";
							
					}
					whereCodes += " ) ";
						
				}

//				//prepare sql
				if(whereCodes.equals(""))
					return result;

				sql.append(
						"	select coalesce(sum(fa.amtacctcr - fa.amtacctdr),0)	\n" +
						"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n"	+ 
						"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	+	
						"		" + whereCodes
						);
				
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
				if(result.compareTo(zero) != 0 )
					result = result.divide(new BigDecimal(1000));
				
				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
				
				return result; 
			}	
			
			private List<Line> load23(MBCSFRInputFormCompany company) {
		 		List<Line> list = new ArrayList<Line>();
				
				MBCSFRInputFormPeriod period = new MBCSFRInputFormPeriod(Env.getCtx(), company.getBCS_FRInputForm_Period_ID(), null);
				int C_Period_ID = period.getPeriodTo();
				MPeriod p = new MPeriod(Env.getCtx(), C_Period_ID, null);
				Long time;

				Timestamp ts1 = p.getEndDate();//begin of choosen period 
				//ts2
				Timestamp ts2 = p.getEndDate();//choosen period 
				
				//ts1
				Date d2 = new Date(ts2.getTime());
				Calendar c = Calendar.getInstance();
				c.setTime(new Date(ts2.getTime()));
				c.set(Calendar.DAY_OF_MONTH, 1);
				c.set(Calendar.MONTH, 0);
				Date d1 = new Date(c.getTimeInMillis());

				String codes = "";
				
				BigDecimal r10 = zero;	BigDecimal r11 = zero;	BigDecimal r12 = zero;
				BigDecimal r20 = zero;	BigDecimal r21 = zero;	BigDecimal r22 = zero;
			
				codes =	"281000";	
				r10 = getData23(codes, new Timestamp(d1.getTime()), null, "saldo");
				r11 = getData23(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"saldo-");
				r12 = getData23(codes, null, new Timestamp(d2.getTime()),"saldo");
				
				codes = "431000";	
				r20 = getData23(codes, null, new Timestamp(d1.getTime()),"saldo");
				r21 = getData23(codes, new Timestamp(d1.getTime()), new Timestamp(d2.getTime()),"saldo-");
				r22 = getData23(codes, null, new Timestamp(d2.getTime()),"saldo");

				
				Line line;
				/*	1	*/	line = new Line();		line.setValue1(r10.toString()); line.setValue2(r11.toString()); line.setValue3(r12.toString()); list.add(line);
				/*	2	*/	line = new Line();		line.setValue1(r20.toString()); line.setValue2(r21.toString()); line.setValue3(r22.toString()); list.add(line);		
				
				return list;
			}

			
			private BigDecimal getData23(String codes, Date dateFrom, Date dateTo, String type){
				BigDecimal result = zero;
				StringBuffer sql = new StringBuffer();
				
				String[] words = codes.split(",");  
				
				String whereCodes = "";
				if(words.length > 0){
					whereCodes = " and ( ";
					for(int i=0; i < words.length; i++){
						if(i == (words.length-1))
							whereCodes += "	ce.value like '"+words[i]+"%'	"; 
						else
							whereCodes += "	ce.value like '"+words[i]+"%' or ";
							
					}
					whereCodes += " ) ";
						
				}

//				//prepare sql
				if(whereCodes.equals(""))
					return result;

				if(type.equals("saldo"))
					sql.append("	select coalesce(sum(fa.amtacctcr + fa.amtacctdr),0)	\n");
				else if(type.equals("saldo-"))
					sql.append("	select coalesce(sum(fa.amtacctcr - fa.amtacctdr),0)	\n");

				
				sql.append(
						//"	select coalesce(sum(fa.amtacctcr + fa.amtacctdr),0)	\n" +
						"		from adempiere.fact_acct fa, adempiere.c_elementvalue ce where fa.account_id = ce.c_elementvalue_id	\n" 
						);
				
				
				if(dateFrom != null && dateTo != null){	
					sql.append(	"		and fa.dateacct between '"+dateFrom+"'::date and '"+dateTo+"'::date	\n"	);
				}else if(dateTo == null){
					sql.append(	"		and fa.dateacct <= '"+dateFrom+"'::date 	\n"	);
				}if(dateFrom == null){
					sql.append(	"		and fa.dateacct <= '"+dateTo+"'::date	\n"	);
				}
				
					sql.append(whereCodes);				
				
				PreparedStatement pstmt = null;
				ResultSet rs = null;
				
				try {
					pstmt = DB.prepareStatement(sql.toString(), null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
							result = rs.getBigDecimal(1);
					}
				} catch (SQLException e) {
					s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
				} finally {
					DB.close(rs, pstmt);
					rs = null;
					pstmt = null;
				}
				
				if(result.compareTo(zero) != 0 )
					result = result.divide(new BigDecimal(1000));
				
				result = result.setScale(0, BigDecimal.ROUND_HALF_UP);
				
				return result; 
			}	
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
	//----------------------------------------------------------------------
			
	private List<Line> read1(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 17)
				continue;

			if (row.getRowNum() == 47)
				continue;

			
			for (Cell cell : row) {
				if (cell.getColumnIndex() == 2 || cell.getColumnIndex() == 3) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
//						value.longValue()
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					} 
					else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 2)
						line.setValue1(s);
					else
						line.setValue2(s);	
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	private List<Line> read2(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 8)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() > 2 || cell.getColumnIndex() < 11) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 3)
						line.setValue1(s);
					if(cell.getColumnIndex() == 4)
						line.setValue2(s);
					if(cell.getColumnIndex() == 5)
						line.setValue3(s);
					if(cell.getColumnIndex() == 6)
						line.setValue4(s);
					if(cell.getColumnIndex() == 7)
						line.setValue5(s);
					if(cell.getColumnIndex() == 8)
						line.setValue6(s);
					if(cell.getColumnIndex() == 9)
						line.setValue7(s);
					if(cell.getColumnIndex() == 10)
						line.setValue8(s);

				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}

	private List<Line> read3(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 9)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() == 2 || cell.getColumnIndex() == 3) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 2)
						line.setValue1(s);
					else
						line.setValue2(s);	
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	private List<Line> read4(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 7)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() > 7 || cell.getColumnIndex() < 16) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 8)
						line.setValue1(s);
					if(cell.getColumnIndex() == 9)
						line.setValue2(s);
					if(cell.getColumnIndex() == 10)
						line.setValue3(s);
					if(cell.getColumnIndex() == 11)
						line.setValue4(s);
					if(cell.getColumnIndex() == 12)
						line.setValue5(s);
					if(cell.getColumnIndex() == 13)
						line.setValue6(s);
					if(cell.getColumnIndex() == 14)
						line.setValue7(s);
					if(cell.getColumnIndex() == 15)
						line.setValue8(s);

				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	private List<Line> read5(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 10)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() == 2 || cell.getColumnIndex() == 3) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 2)
						line.setValue1(s);
					else
						line.setValue2(s);	
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	private List<Line> read6(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 7)
				continue;
			boolean is = false;
			for (Cell cell : row) {
				if(cell.getColumnIndex() == 0 && cell.getCellType() == Cell.CELL_TYPE_STRING){
					String s2 = cell.getStringCellValue();
					if(s2.equals(""))
						continue;
					else
						is = true;
				}
						
				if (cell.getColumnIndex() > 1 || cell.getColumnIndex() < 10) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					} else if(is) s = "0";
					else continue;
					
					
//					if (s.equals(""))
//						continue;
					
					if(cell.getColumnIndex() == 2)
						line.setValue1(s);
					if(cell.getColumnIndex() == 3)
						line.setValue2(s);
					if(cell.getColumnIndex() == 4)
						line.setValue3(s);
					if(cell.getColumnIndex() == 5)
						line.setValue4(s);
					if(cell.getColumnIndex() == 6)
						line.setValue5(s);
					if(cell.getColumnIndex() == 7)
						line.setValue6(s);
					if(cell.getColumnIndex() == 8)
						line.setValue7(s);
					if(cell.getColumnIndex() == 9)
						line.setValue8(s);

				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}

	private List<Line> read7(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 10)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() > 1 || cell.getColumnIndex() < 9) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 2)
						line.setValue1(s);
					if(cell.getColumnIndex() == 3)
						line.setValue2(s);
					if(cell.getColumnIndex() == 4)
						line.setValue3(s);
					if(cell.getColumnIndex() == 5)
						line.setValue4(s);
					if(cell.getColumnIndex() == 6)
						line.setValue5(s);
					if(cell.getColumnIndex() == 7)
						line.setValue6(s);
					if(cell.getColumnIndex() == 8)
						line.setValue7(s);
//					if(cell.getColumnIndex() == 9)
//						line.setValue8(s);

				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	private List<Line> read8(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 9 || row.getRowNum() == 10)
				continue;

//			System.out.println(row.getRowNum());
	
			for (Cell cell : row) {
//				s = "";
				if (cell.getColumnIndex() == 2 || cell.getColumnIndex() == 3 ) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC && cell.getColumnIndex() == 3){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA && cell.getColumnIndex() == 3){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}
					
					else if (cell.getCellType() == Cell.CELL_TYPE_STRING && cell.getColumnIndex() == 2){
						s = cell.getStringCellValue();
					}
					
					else s = "";//continue;
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 3)
						line.setValue1(s);
					if(cell.getColumnIndex() == 2)
						line.setValue2(s);
//					if(cell.getColumnIndex() == 5)
//						line.setValue3(s);
//					if(cell.getColumnIndex() == 6)
//						line.setValue4(s);
//					if(cell.getColumnIndex() == 7)
//						line.setValue5(s);
//					if(cell.getColumnIndex() == 8)
//						line.setValue6(s);
//					if(cell.getColumnIndex() == 9)
//						line.setValue7(s);
//					if(cell.getColumnIndex() == 10)
//						line.setValue8(s);
//					if(cell.getColumnIndex() == 11)
//						line.setValue9(s);
//					if(cell.getColumnIndex() == 12)
//						line.setValue10(s);
//					if(cell.getColumnIndex() == 13)
//						line.setValue11(s);
					
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	
	private List<Line> read9(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (
					row.getRowNum() < 9
					|| row.getRowNum() == 50 || row.getRowNum() == 51 
					|| row.getRowNum() == 52 || row.getRowNum() == 53
					|| row.getRowNum() == 54 
//					&& row.getRowNum() != 55 
//					&& row.getRowNum() != 56 && row.getRowNum() != 57
					//&& row.getRowNum() != 58
				)
				continue;

			int irow = row.getRowNum();
			if(irow == 55)
				irow = irow;
			
			for (Cell cell : row) {
//				s = "";
				if (cell.getColumnIndex() > 2 && cell.getColumnIndex() < 14) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 3)
						line.setValue1(s);
					if(cell.getColumnIndex() == 4)
						line.setValue2(s);
					if(cell.getColumnIndex() == 5)
						line.setValue3(s);
					if(cell.getColumnIndex() == 6)
						line.setValue4(s);
					if(cell.getColumnIndex() == 7)
						line.setValue5(s);
					if(cell.getColumnIndex() == 8)
						line.setValue6(s);
					if(cell.getColumnIndex() == 9)
						line.setValue7(s);
					if(cell.getColumnIndex() == 10)
						line.setValue8(s);
					if(cell.getColumnIndex() == 11)
						line.setValue9(s);
					if(cell.getColumnIndex() == 12)
						line.setValue10(s);
					if(cell.getColumnIndex() == 13)
						line.setValue11(s);
					
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	private List<Line> read11(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 10)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() == 4) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 4)
						line.setValue1(s);
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}

	private List<Line> read12(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;
//		System.out.println("---------------------------------------");
		String s = "";
		boolean end = false;
		
		for (Row row : sheet) {
			if(end) break;
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 10)
				continue;
			
//			System.out.println(row.getRowNum());
			for (Cell cell : row) {
				s = "";
				if(cell.getColumnIndex() == 1){
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						double value = cell.getNumericCellValue();
//						s = Double.toString(value);
						if(value == 999)
							end = true;
					}
				}
//					if(end) break;				
				
				if (cell.getColumnIndex() >1 && cell.getColumnIndex() < 20) {
					
//					Date d = cell.getDataCellValue();
//					if(cell.getDataCellValue() == Cell.CELL_TYPE_NUMERIC){
//						HSSFDateUtil.isCellDateFormatted(cell)
////						CellValue cValue = formula.evaluate(cell);
////						double dv = cValue.getNumberValue();
//						Double value = cell.getNumericCellValue();
//					    Date date = HSSFDateUtil.getJavaDate(value);
//					    String dateFmt = cell.getCellStyle().getDataFormatString();
//
//					    s = new CellDateFormatter(dateFmt).format(date);
//					}else
					if(cell.getColumnIndex() == 6){
						if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cell.getNumericCellValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}else	if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
							HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
							CellValue cv = ev.evaluate(cell);
							if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
								Double value = cv.getNumberValue();
								s = Long.toString(value.longValue());// Double.toString(value);
							}	
						}
						
					}else if (cell.getCellType() == Cell.CELL_TYPE_STRING){
						s = cell.getStringCellValue();
					} else	if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						if(HSSFDateUtil.isCellDateFormatted(cell))//HSSFDateUtil.isValidExcelDate(value))
						{
							Date date = HSSFDateUtil.getJavaDate(value);
							SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");	
							s = dateFormat.format(date);								
						}
						else
						{
							s = Long.toString(value.longValue());// Double.toString(value);
						}
				
						
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 2)
						line.setValue1(s);
					if(cell.getColumnIndex() == 3)
						line.setValue2(s);
					if(cell.getColumnIndex() == 4)
						line.setValue3(s);
					if(cell.getColumnIndex() == 5)
						line.setValue4(s);
					if(cell.getColumnIndex() == 6)
						line.setValue5(s);
					if(cell.getColumnIndex() == 7)
						line.setValue6(s);
					if(cell.getColumnIndex() == 8)
						line.setValue7(s);
					if(cell.getColumnIndex() == 9)
						line.setValue8(s);
					if(cell.getColumnIndex() == 10)
						line.setValue9(s);
					if(cell.getColumnIndex() == 11)
						line.setValue10(s);
					if(cell.getColumnIndex() == 12)
						line.setValue11(s);
					if(cell.getColumnIndex() == 13)
						line.setValue12(s);
					if(cell.getColumnIndex() == 14)
						line.setValue13(s);

				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	
	private List<Line> read13(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 9)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() > 0 && cell.getColumnIndex() < 8) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 1)
						line.setValue1(s);
					if(cell.getColumnIndex() == 2)
						line.setValue2(s);
					if(cell.getColumnIndex() == 3)
						line.setValue3(s);		
					if(cell.getColumnIndex() == 4)
						line.setValue4(s);		
					if(cell.getColumnIndex() == 5)
						line.setValue5(s);		
					if(cell.getColumnIndex() == 6)
						line.setValue6(s);			
					if(cell.getColumnIndex() == 7)
						line.setValue7(s);					
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}

	private List<Line> read14(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 10)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() > 0 && cell.getColumnIndex() < 7) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 1)
						line.setValue1(s);
					if(cell.getColumnIndex() == 2)
						line.setValue2(s);
					if(cell.getColumnIndex() == 3)
						line.setValue3(s);		
					if(cell.getColumnIndex() == 4)
						line.setValue4(s);		
					if(cell.getColumnIndex() == 5)
						line.setValue5(s);		
					if(cell.getColumnIndex() == 6)
						line.setValue6(s);			
					if(cell.getColumnIndex() == 7)
						line.setValue7(s);					
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	private List<Line> read16(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 9)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() > 0 && cell.getColumnIndex() < 7) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 1)
						line.setValue1(s);
					if(cell.getColumnIndex() == 2)
						line.setValue2(s);
					if(cell.getColumnIndex() == 3)
						line.setValue3(s);		
					if(cell.getColumnIndex() == 4)
						line.setValue4(s);		
					if(cell.getColumnIndex() == 5)
						line.setValue5(s);		
					if(cell.getColumnIndex() == 6)
						line.setValue6(s);			
					if(cell.getColumnIndex() == 7)
						line.setValue7(s);					
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	
	private List<Line> read18(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 8)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() == 2 || cell.getColumnIndex() == 3) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if(cell.getColumnIndex() == 3){
						if (cell.getCellType() == Cell.CELL_TYPE_STRING)
							s = cell.getStringCellValue();
					}

					
					if (s.equals(""))
						continue;

					if(cell.getColumnIndex() == 2)
						line.setValue1(s);
					else 
						line.setValue2(s);
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	
	private List<Line> read17(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 9)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() > 0 || cell.getColumnIndex() < 5) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;

					switch(cell.getColumnIndex()){	
						case 1:	
							line.setValue1(s);
							break;
						case 2:	
							line.setValue2(s);
							break;
						case 3:	
							line.setValue3(s);
							break;
						case 4:	
							line.setValue4(s);
							break;
						case 5:	
							line.setValue5(s);
							break;							
					}
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	
	private List<Line> read15(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 11)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() == 5) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
//						double value = cell.getNumericCellValue();
//						s = Double.toString(value);
						Double value = cell.getNumericCellValue();
//						if(HSSFDateUtil.isValidExcelDate(value))
//						{
//							Date date = HSSFDateUtil.getJavaDate(value);
//							SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");	
//							s = dateFormat.format(date);								
//						}
//						else
//						{
						s = Long.toString(value.longValue());// Double.toString(value);
//						}						
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;

						line.setValue1(s);
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	
	private List<Line> read10(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			boolean is = false;
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 9)
				continue;

			for (Cell cell : row) {
			
				if (cell.getColumnIndex() == 2 || cell.getColumnIndex() == 3) {
					
					s = "";
					
					if(cell.getColumnIndex() == 2){
						if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cell.getNumericCellValue();
							s = Long.toString(value.longValue());// Double.toString(value);
							is = true;
						}else 
	
						if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
							HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
							CellValue cv = ev.evaluate(cell);
							if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
								Double value = cv.getNumberValue();
								s = Long.toString(value.longValue());// Double.toString(value);
								is = true;
							}						
						}else s = "0";//continue;
					}else
					
					if(cell.getColumnIndex() == 3){
						if (cell.getCellType() == Cell.CELL_TYPE_STRING)
							s = cell.getStringCellValue();
						is = true;
					}
					else s = "0";
						
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 2)
						line.setValue1(s);
					else 
						line.setValue2(s);
				}

			}

			if (is)
				list.add(line);
		}
		return list;

	}

	private List<Line> read19(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;
//		System.out.println("---------------------------------------");
		String s = "";
		boolean end = false;
		
		for (Row row : sheet) {
			if(end) break;
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 10 || row.getRowNum() == 11)
				continue;
			
//			System.out.println(row.getRowNum());
			for (Cell cell : row) {
				
				if(cell.getColumnIndex() == 5){
					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA || cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
//						s = Double.toString(value); 
						if(value == .0)
							end = true;
					}else end = true;
				}
					if(end) break;				
				
				if (cell.getColumnIndex() >1 && cell.getColumnIndex() < 7) {
					
					if(cell.getColumnIndex() == 5 && cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cell.getNumericCellValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}					
					}else
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
//						double value = cell.getNumericCellValue();
//						s = Double.toString(value);
						Double value = cell.getNumericCellValue();
						if(HSSFDateUtil.isValidExcelDate(value))
						{
							Date date = HSSFDateUtil.getJavaDate(value);
							SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");	
							s = dateFormat.format(date);								
						}
						else
						{
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					} else
					if (cell.getCellType() == Cell.CELL_TYPE_STRING){
							s = cell.getStringCellValue();
					}else continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 2)
						line.setValue1(s);
					if(cell.getColumnIndex() == 3)
						line.setValue2(s);
					if(cell.getColumnIndex() == 4)
						line.setValue3(s);
					if(cell.getColumnIndex() == 5)
						line.setValue4(s);
					if(cell.getColumnIndex() == 6)
						line.setValue5(s);
					if(cell.getColumnIndex() == 7)
						line.setValue6(s);


				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	
	private List<Line> read20(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 7)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() == 4 || cell.getColumnIndex() == 5) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if(cell.getColumnIndex() == 5){
						if (cell.getCellType() == Cell.CELL_TYPE_STRING)
							s = cell.getStringCellValue();
					}
						
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 4)
						line.setValue1(s);
					else 
						line.setValue2(s);
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}

	private List<Line> read23(HSSFSheet sheet, List<Line> list) {
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 8)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() > 0 && cell.getColumnIndex() < 4) {
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 1)
						line.setValue1(s);
					if(cell.getColumnIndex() == 2)
						line.setValue2(s);
					if(cell.getColumnIndex() == 3)
						line.setValue3(s);					
				}

			}

			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;

	}
	
	private List<MBCSFRInputFormColumn> getMBCSFRInputFormColumn(
			List<MBCSFRInputFormLine> lines) {
		
		List<MBCSFRInputFormColumn> columns = new ArrayList<MBCSFRInputFormColumn>();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		for(MBCSFRInputFormLine line: lines){
		
			sql = "SELECT BCS_FRInputFormColumn_ID FROM adempiere.BCS_FRInputFormColumn WHERE  BCS_FRInputFormLine_ID = " + line.get_ID() 
					+ " order by seqno";
	
			try {
				pstmt = DB.prepareStatement(sql, null);
				rs = pstmt.executeQuery();
				while(rs.next()) {
					MBCSFRInputFormColumn column = new MBCSFRInputFormColumn(Env.getCtx(), rs.getInt(1),	null);
					columns.add(column);
				}
			} catch (SQLException e) {
				s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
			} finally {
				DB.close(rs, pstmt);
				rs = null;
				pstmt = null;
			}
		}
		return columns;
	}
	

	private List<MBCSFRInputFormLine> getAllForms_MBCSFRInputFormLines(
			List<MBCSFRInputForm> forms) {
		
		List<MBCSFRInputFormLine> lines = new ArrayList<MBCSFRInputFormLine>();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		for(MBCSFRInputForm form: forms){
		
			sql = "SELECT BCS_FRInputFormLine_ID FROM adempiere.BCS_FRInputFormLine WHERE  BCS_FRInputForm_ID = " + form.get_ID()
					+ " order by BCS_FRInputFormLine_ID";
	
			try {
				pstmt = DB.prepareStatement(sql, null);
				rs = pstmt.executeQuery();
				while(rs.next()) {
					MBCSFRInputFormLine line = new MBCSFRInputFormLine(Env.getCtx(), rs.getInt(1),	null);
					lines.add(line);
				}
			} catch (SQLException e) {
				s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
			} finally {
				DB.close(rs, pstmt);
				rs = null;
				pstmt = null;
			}
		}
		return lines;
	}

	private List<MBCSFRInputFormLine> getMBCSFRInputFormLines(MBCSFRInputForm form) {
		
		List<MBCSFRInputFormLine> lines = new ArrayList<MBCSFRInputFormLine>();
		if(form == null)
			return lines;
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
//		for(MBCSFRInputForm form: forms){
		
			sql = "SELECT BCS_FRInputFormLine_ID FROM adempiere.BCS_FRInputFormLine WHERE  BCS_FRInputForm_ID = " + form.get_ID()
					+ " order by BCS_FRInputFormLine_ID";
	
			try {
				pstmt = DB.prepareStatement(sql, null);
				rs = pstmt.executeQuery();
				while(rs.next()) {
					MBCSFRInputFormLine line = new MBCSFRInputFormLine(Env.getCtx(), rs.getInt(1),	null);
					lines.add(line);
				}
			} catch (SQLException e) {
				s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
			} finally {
				DB.close(rs, pstmt);
				rs = null;
				pstmt = null;
			}
//		}
		return lines;
	}
	
	
	
	private List<MBCSFRInputForm> getMBCSFRInputForm(
			MBCSFRInputFormCompany company) {
		
		List<MBCSFRInputForm> forms = new ArrayList<MBCSFRInputForm>();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";

		//sql = "SELECT BCS_FRInputForm_ID FROM adempiere.BCS_FRInputForm WHERE  BCS_FRInputForm_Company_ID = " + company.get_ID();
		sql = "	SELECT f.BCS_FRInputForm_ID, f.BCS_FRInputForm_Company_ID, s.bcs_frsetupinputform_id, s.code, s.isIntercompanySales	\n" 
				+	"	FROM adempiere.BCS_FRInputForm f	\n"
				+	"	inner join adempiere.bcs_frsetupinputform s on s.bcs_frsetupinputform_id = f.bcs_frsetupinputform_id	\n"
				+	"	WHERE  f.BCS_FRInputForm_Company_ID =	"	+company.get_ID();

		try {
			pstmt = DB.prepareStatement(sql, null);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				MBCSFRInputForm form = new MBCSFRInputForm(Env.getCtx(), rs.getInt(1),	null);
				form.setCode(rs.getInt(4));
				form.setIntercompanySales(rs.getBoolean(5));
				forms.add(form);
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "CalloutBCS_FRInputCompanyUpload", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}
//		

		return forms;
	}

	private List<Line> read22(HSSFSheet sheet, List<Line> list) {//вознаграждение работникам
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 8)
				continue;

			for (Cell cell : row) {
				s = "";
				
				if (cell.getColumnIndex() == 4 || cell.getColumnIndex() == 5) {
//					CellReference cellRef = new CellReference(row.getRowNum(),
//							cell.getColumnIndex());
//					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
//					if (cell.getCellType() == Cell.CELL_TYPE_STRING)
//						s = cell.getStringCellValue();
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 

					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cv.getNumberValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
					
					if(cell.getColumnIndex() == 5){
						if (cell.getCellType() == Cell.CELL_TYPE_STRING)
							s = cell.getStringCellValue();
					}
						
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 4)
						line.setValue1(s);
					else 
						line.setValue2(s);
				
				
				}				

			}
//			System.out.println();
			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;
	}
	
	
	private List<Line> read21(HSSFSheet sheet, List<Line> list) {//вознаграждение работникам
		if(sheet == null) return null;

		String s = "";
		for (Row row : sheet) {
			
			s = "";
			Line line = new Line();
			line.setSheet(0);

			if (row.getRowNum() < 8)
				continue;

			for (Cell cell : row) {
				if (cell.getColumnIndex() == 6 || cell.getColumnIndex() == 7) {
//					CellReference cellRef = new CellReference(row.getRowNum(),
//							cell.getColumnIndex());
//					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
//					if (cell.getCellType() == Cell.CELL_TYPE_STRING)
////						s = cell.getStringCellValue();
//						continue;
					
					if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
						Double value = cell.getNumericCellValue();
						s = Long.toString(value.longValue());// Double.toString(value);
					}else 
					
					if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){
						HSSFFormulaEvaluator ev = new HSSFFormulaEvaluator(wb);
						CellValue cv = ev.evaluate(cell);
//						System.out.println();
						if (cv.getCellType() == Cell.CELL_TYPE_NUMERIC){
							Double value = cell.getNumericCellValue();
							s = Long.toString(value.longValue());// Double.toString(value);
						}						
					}else s = "0";//continue;
						
					
					if(cell.getColumnIndex() == 7){
						if (cell.getCellType() == Cell.CELL_TYPE_STRING)
							s = cell.getStringCellValue();
					}
						
					
					
					if (s.equals(""))
						continue;
					
					if(cell.getColumnIndex() == 6)
						line.setValue1(s);
					else 
						line.setValue2(s);
				}

			}
//			System.out.println();
			if (s.equals(""))
				continue;
			list.add(line);
		}
		return list;
	}

	
	class iline{
		private String code = "";
		private BigDecimal f1 = zero;
		private BigDecimal f2 = zero;
		private BigDecimal f3 = zero;
		private BigDecimal f4 = zero;
		
		private BigDecimal f5 = zero;
		private BigDecimal f6 = zero;
		private BigDecimal f7 = zero;
		private BigDecimal f8 = zero;
		
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		public BigDecimal getF1() {
			return f1;
		}
		public void setF1(BigDecimal f1) {
			this.f1 = f1;
		}
		public BigDecimal getF2() {
			return f2;
		}
		public void setF2(BigDecimal f2) {
			this.f2 = f2;
		}
		public BigDecimal getF3() {
			return f3;
		}
		public void setF3(BigDecimal f3) {
			this.f3 = f3;
		}
		public BigDecimal getF4() {
			return f4;
		}
		public void setF4(BigDecimal f4) {
			this.f4 = f4;
		}
		public BigDecimal getF5() {
			return f5;
		}
		public void setF5(BigDecimal f5) {
			this.f5 = f5;
		}
		public BigDecimal getF6() {
			return f6;
		}
		public void setF6(BigDecimal f6) {
			this.f6 = f6;
		}
		public BigDecimal getF7() {
			return f7;
		}
		public void setF7(BigDecimal f7) {
			this.f7 = f7;
		}
		public BigDecimal getF8() {
			return f8;
		}
		public void setF8(BigDecimal f8) {
			this.f8 = f8;
		}
		
		
	}
	
	class TablesWithC_charge_ID{
		int AD_Table_ID = -1;
		String TableName = "";
		
		public int getAD_Table_ID() {
			return AD_Table_ID;
		}
		public void setAD_Table_ID(int aD_Table_ID) {
			AD_Table_ID = aD_Table_ID;
		}
		public String getTableName() {
			return TableName;
		}
		public void setTableName(String tableName) {
			TableName = tableName;
		}
		
	}
	
	class Line {
		int sheet;
		String Name;
		String value1;
		String value2;
		String value3;
		String value4;
		String value5;
		String value6;
		String value7;
		String value8;
		String value9;
		String value10;
		String value11;
		String value12;
		String value13;
		
		Line(int s, String name, String v1, String v2, String v3, String v4, String v5, 
				String v6, String v7, String v8, String v9, String v10, String v11,
				String v12, String v13
				) {
			this.sheet = s;
			this.value1 = v1;
			this.value2 = v2;
			this.value3 = v3;
			this.value4 = v4;
			this.value5 = v5;
			this.value6 = v6;
			this.value7 = v7;
			this.value8 = v8;
			this.value9 = v9;
			this.value10 = v10;
			this.value11 = v11;
			this.value12 = v12;
			this.value13 = v13;
		}

		Line() {
			this.sheet = 0;
			this.Name = "";
			this.value1 = "";
			this.value2 = "";
			this.value3 = "";
			this.value4 = "";
			this.value5 = "";
			this.value6 = "";
			this.value7 = "";
			this.value8 = "";
			this.value9 = "";
			this.value10 = "";
			this.value11 = "";
			this.value12 = "";
			this.value13 = "";			
		}

		public String getValue12() {
			return value12;
		}

		public void setValue12(String value12) {
			this.value12 = value12;
		}

		public String getValue13() {
			return value13;
		}

		public void setValue13(String value13) {
			this.value13 = value13;
		}

		public String getName() {
			return Name;
		}

		public void setName(String name) {
			Name = name;
		}

		public int getSheet() {
			return sheet;
		}

		public void setSheet(int sheet) {
			this.sheet = sheet;
		}

		public String getValue1() {
			return value1;
		}

		public void setValue1(String value1) {
			this.value1 = value1;
		}

		public String getValue2() {
			return value2;
		}

		public void setValue2(String value2) {
			this.value2 = value2;
		}

		public String getValue3() {
			return value3;
		}

		public void setValue3(String value3) {
			this.value3 = value3;
		}

		public String getValue4() {
			return value4;
		}

		public void setValue4(String value4) {
			this.value4 = value4;
		}

		public String getValue5() {
			return value5;
		}

		public void setValue5(String value5) {
			this.value5 = value5;
		}

		public String getValue6() {
			return value6;
		}

		public void setValue6(String value6) {
			this.value6 = value6;
		}

		public String getValue7() {
			return value7;
		}

		public void setValue7(String value7) {
			this.value7 = value7;
		}

		public String getValue8() {
			return value8;
		}

		public void setValue8(String value8) {
			this.value8 = value8;
		}

		public String getValue9() {
			return value9;
		}

		public void setValue9(String value9) {
			this.value9 = value9;
		}

		public String getValue10() {
			return value10;
		}

		public void setValue10(String value10) {
			this.value10 = value10;
		}

		public String getValue11() {
			return value11;
		}

		public void setValue11(String value11) {
			this.value11 = value11;
		}

		
		
	}
	

}
